Skip to content

Commit ef3b611

Browse files
authored
feat: add two more linkedlist methods (careercup#63)
- insertAt(index, value) - removeAt(index)
1 parent 83223c2 commit ef3b611

File tree

4 files changed

+406
-167
lines changed

4 files changed

+406
-167
lines changed

chapter02/util/LinkedListX.js

Lines changed: 75 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ class Node {
1818
this.value = value
1919
this.next = null;
2020
}
21-
}
22-
23-
class LinkedList {
21+
}
22+
23+
class LinkedList {
2424
constructor() {
2525
this.head = null;
2626
this.tail = null;
2727
}
28-
28+
2929
append(value) {
3030
let node = new Node(value);
3131
// if list is empty
@@ -38,16 +38,16 @@ class LinkedList {
3838
this.tail = node;
3939
}
4040
}
41-
41+
4242
prepend(value) {
4343
let node = new Node(value);
4444
node.next = this.head;
4545
this.head = node;
4646
}
47-
47+
4848
pop() {
4949
let cur = this.head;
50-
50+
5151
// only one or no item exists
5252
if (!cur) return null;
5353
if (!cur.next) {
@@ -63,7 +63,7 @@ class LinkedList {
6363
this.tail.next = null;
6464
return last;
6565
}
66-
66+
6767
popFirst() {
6868
let first = this.head;
6969
if (this.head && this.head.next) {
@@ -73,25 +73,85 @@ class LinkedList {
7373
else this.head = null;
7474
return first;
7575
}
76-
76+
7777
head() {
7878
return this.head;
7979
}
80-
80+
81+
removeAt(index) {
82+
let i = 0;
83+
let cur = this.head;
84+
let prev = null;
85+
86+
while (cur != null) {
87+
if (i == index) {
88+
// remove
89+
if (prev == null)
90+
this.head = cur.next;
91+
else prev.next = cur.next;
92+
cur.next = null;
93+
return cur.value;
94+
}
95+
else {
96+
prev = cur;
97+
cur = cur.next;
98+
i++;
99+
}
100+
}
101+
return null;
102+
}
103+
104+
insertAt(index, value) {
105+
if (index == 0) return this.prepend(value);
106+
let cur = this.head;
107+
let i = 0;
108+
109+
while (cur != null) {
110+
if (i == index - 1) {
111+
let node = new Node(value);
112+
node.next = cur.next;
113+
cur.next = node;
114+
return true;
115+
}
116+
else {
117+
i++;
118+
cur = cur.next;
119+
}
120+
}
121+
return false;
122+
}
123+
81124
tail() {
82125
return this.tail;
83126
}
84-
127+
85128
_toArray() {
86129
let arr = [];
87130
let cur = this.head;
88131
while (cur) {
89132
arr.push(cur.value);
90133
cur = cur.next;
91134
}
92-
135+
93136
return arr;
94137
}
95-
}
96-
97-
module.exports = LinkedList;
138+
}
139+
140+
module.exports = LinkedList;
141+
142+
/* TEST */
143+
144+
// let l = new LinkedList();
145+
// l.append(3);
146+
// l.append(4);
147+
// l.append(10);
148+
// l.append(20);
149+
// l.append(5);
150+
151+
// console.log(l.removeAt(1), 4);
152+
// console.log(l.pop().value, 5);
153+
154+
// console.log(l._toArray());
155+
// l.insertAt(2, 40);
156+
// console.log(l._toArray());
157+

chapter03/util/LinkedList.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,49 @@ class LinkedList {
6565
return this.head;
6666
}
6767

68+
removeAt(index) {
69+
let i = 0;
70+
let cur = this.head;
71+
let prev = null;
72+
73+
while (cur != null) {
74+
if (i == index) {
75+
// remove
76+
if (prev == null)
77+
this.head = cur.next;
78+
else prev.next = cur.next;
79+
cur.next = null;
80+
return cur.value;
81+
}
82+
else {
83+
prev = cur;
84+
cur = cur.next;
85+
i++;
86+
}
87+
}
88+
return null;
89+
}
90+
91+
insertAt(index, value) {
92+
if (index == 0) return this.prepend(value);
93+
let cur = this.head;
94+
let i = 0;
95+
96+
while (cur != null) {
97+
if (i == index - 1) {
98+
let node = new Node(value);
99+
node.next = cur.next;
100+
cur.next = node;
101+
return true;
102+
}
103+
else {
104+
i++;
105+
cur = cur.next;
106+
}
107+
}
108+
return false;
109+
}
110+
68111
tail() {
69112
return this.tail;
70113
}
@@ -82,3 +125,19 @@ class LinkedList {
82125
}
83126

84127
module.exports = LinkedList;
128+
129+
/* TEST */
130+
131+
// let l = new LinkedList();
132+
// l.append(3);
133+
// l.append(4);
134+
// l.append(10);
135+
// l.append(20);
136+
// l.append(5);
137+
138+
// console.log(l.removeAt(1), 4);
139+
// console.log(l.pop().value, 5);
140+
141+
// console.log(l._toArray());
142+
// l.insertAt(2, 40);
143+
// console.log(l._toArray());

0 commit comments

Comments
 (0)