File tree 1 file changed +22
-5
lines changed
src/_DataStructures_/LinkedList
1 file changed +22
-5
lines changed Original file line number Diff line number Diff line change @@ -126,24 +126,27 @@ class LinkedList {
126
126
if ( ! this . head ) {
127
127
return null ;
128
128
}
129
+ if ( index === 0 ) {
130
+ return this . removeFromBeginning ( ) ;
131
+ }
129
132
130
- if ( index >= this . length ( ) ) {
133
+ if ( index >= this . size - 1 ) {
131
134
return this . removeFromEnd ( ) ;
132
135
}
133
136
134
137
let address = this . head ;
135
138
let previous = address ;
136
139
let count = index ;
137
140
138
- while ( count ) {
139
- address = address . next ;
141
+ while ( count >= 1 ) {
140
142
previous = address ;
143
+ address = address . next ;
141
144
count -= 1 ;
142
145
}
143
-
144
146
const node = address ;
145
- previous . next = address . next . next ;
147
+ previous . next = address . next ;
146
148
this . size -= 1 ;
149
+
147
150
node . next = null ;
148
151
return node ;
149
152
}
@@ -169,4 +172,18 @@ class LinkedList {
169
172
}
170
173
}
171
174
175
+ // const ll = new LinkedList();
176
+ // ll.addAtBeginning(20);
177
+ // ll.addAtBeginning(15);
178
+ // ll.addAtBeginning(10);
179
+ // ll.addAtBeginning(5);
180
+
181
+ // console.log(ll.traverseList());
182
+
183
+ // console.log(ll.removeAt(0));
184
+ // console.log(ll.traverseList());
185
+
186
+ // console.log(ll.removeAt(1));
187
+ // console.log(ll.traverseList());
188
+
172
189
module . exports = { LinkedList, Node } ;
You can’t perform that action at this time.
0 commit comments