@@ -15,7 +15,8 @@ class LinkedList {
15
15
}
16
16
17
17
insertFirst ( data ) {
18
- this . head = new Node ( data , this . head ) ;
18
+ // this.head = new Node(data, this.head);
19
+ this . insertAt ( data , 0 ) ;
19
20
}
20
21
21
22
size ( ) {
@@ -29,61 +30,68 @@ class LinkedList {
29
30
}
30
31
31
32
getFirst ( ) {
32
- return this . head ;
33
+ // return this.head;
34
+ return this . getAt ( 0 ) ;
33
35
}
34
36
35
37
getLast ( ) {
36
- if ( ! this . head ) {
37
- return null ;
38
- }
39
-
40
- let node = this . head ;
41
- while ( node ) {
42
- if ( ! node . next ) {
43
- return node ;
44
- }
45
- node = node . next ;
46
- }
38
+ // if(!this.head) {
39
+ // return null;
40
+ // }
41
+
42
+ // let node = this.head;
43
+ // while(node) {
44
+ // if(!node.next) {
45
+ // return node;
46
+ // }
47
+ // node = node.next;
48
+ // }
49
+
50
+ return this . getAt ( this . size ( ) - 1 ) ;
47
51
}
48
52
49
53
clear ( ) {
50
54
this . head = null ;
51
55
}
52
56
53
57
removeFirst ( ) {
54
- if ( ! this . head ) {
55
- return ;
56
- }
58
+ // if(!this.head) {
59
+ // return;
60
+ // }
57
61
58
- this . head = this . head . next ;
62
+ // this.head = this.head.next;
63
+ this . removeAt ( 0 ) ;
59
64
}
60
65
61
66
removeLast ( ) {
62
67
63
- if ( ! this . head ) {
64
- return ;
65
- } else if ( ! this . head . next ) {
66
- previous = null ;
67
- return ;
68
- }
68
+ // if(!this.head) {
69
+ // return;
70
+ // } else if(!this.head.next) {
71
+ // previous = null;
72
+ // return;
73
+ // }
69
74
70
- let previous = this . head ;
71
- let node = this . head . next ;
75
+ // let previous = this.head;
76
+ // let node = this.head.next;
72
77
73
- while ( node . next ) {
74
- previous = node ;
75
- node = node . next ;
76
- }
78
+ // while(node.next) {
79
+ // previous = node;
80
+ // node = node.next;
81
+ // }
82
+
83
+ // previous.next = null;
77
84
78
- previous . next = null ;
85
+ this . removeAt ( this . size ( ) - 1 ) ;
79
86
}
80
87
81
88
insertLast ( data ) {
82
- if ( ! this . head ) {
83
- this . head = new Node ( data ) ;
84
- } else {
85
- this . getLast ( ) . next = new Node ( data ) ;
86
- }
89
+ // if(!this.head) {
90
+ // this.head = new Node(data);
91
+ // } else {
92
+ // this.getLast().next = new Node(data);
93
+ // }
94
+ this . insertAt ( data , this . size ( ) - 1 ) ;
87
95
}
88
96
89
97
getAt ( index ) {
0 commit comments