-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked-list.js
303 lines (239 loc) · 5.23 KB
/
linked-list.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
class Node {
constructor(val = null, next = null) {
this.val = val;
this.next = next;
}
}
export class LinkedList {
constructor() {
this._head = null;
this._tail = null;
}
append(val) {
const node = new Node(val);
if (this._head == null && this._tail == null) {
this._head = node;
this._tail = node;
return;
}
this._tail.next = node;
this._tail = node;
}
prepend(val) {
const node = new Node(val);
if (this._head == null && this._tail == null) {
this._head = node;
this._tail = node;
return;
}
node.next = this._head;
this._head = node;
}
size() {
let current = this._head;
let numOfNodes = 0;
// this way of traversal is used throughout the class.
while (current != null) {
numOfNodes++;
current = current.next;
}
return numOfNodes;
}
head() {
return this._head;
}
tail() {
return this._tail;
}
pop() {
if (this._tail == null) {
console.warn("Tried to pop tail when it's already null!");
return null;
}
let current = this._head;
// keep moving until you reach the node that's just before the tail.
while (current.next != this._tail) {
current = current.next;
}
// current is now the node that's just before the tail.
const poppedNode = this._tail;
current.next = null;
this._tail = current;
return poppedNode;
}
contains(target) {
let current = this._head;
while (current != null) {
if (current.val === target) return true;
current = current.next;
}
// moved through entire list and didn't find target.
return false;
}
find(target) {
let current = this._head;
let currentIndex = 0;
while (current != null) {
if (current.val === target) return currentIndex;
current = current.next;
currentIndex++;
}
// moved through entire list and didn't find target.
return -1;
}
toString() {
let current = this._head;
let str = "";
while (current != null) {
str += `( ${current.val} ) -> `;
current = current.next;
}
// last node always points to null, so add it to str
str += "null";
return str;
}
at(index) {
let current = this._head;
let currentIndex = 0;
while (current != null) {
if (currentIndex == index) return current;
current = current.next;
currentIndex++;
}
return null;
}
insertAt(val, index) {
// insert at head
if (index == 0) {
this.prepend(val);
return;
}
// else if index is not at head
let current = this._head.next;
let prev = this._head; // keep track of previous node
let currentIndex = 1;
while (current != null) {
if (currentIndex == index) {
const node = new Node(val);
// add new node between current and prev nodes
prev.next = node;
node.next = current;
return;
}
prev = current;
current = current.next;
currentIndex++;
}
console.warn("Warning: Tried to insert at invalid index");
}
clear() {
this._head = null;
this._tail = null;
}
removeAt(index) {
// remove head
if (index == 0) {
if (this._head == null) {
console.warn("Tried to remove head when it's already null!");
return null;
}
if (this.size() == 1) {
const removed = this._head.val;
this.clear(); // just clear when there is only one element.
return removed;
}
const removed = this._head;
this._head = this._head.next;
return removed;
}
// else if index is not at head
let current = this._head.next;
let prev = this._head;
let currentIndex = 1;
while (current != null) {
if (currentIndex == index) {
prev.next = current.next;
return current;
}
prev = current;
current = current.next;
currentIndex++;
}
console.warn("Warning: Tried to remove at invalid index");
return null;
}
toArray() {
let current = this._head;
const arr = [];
while (current != null) {
arr.push(current.val);
current = current.next;
}
return arr;
}
}
export class PairLinkedList extends LinkedList {
constructor() {
super();
}
containsKey(key) {
let current = this._head;
while (current != null) {
if (current.val.key === key) return true;
current = current.next;
}
// moved through entire list and didn't find target.
return false;
}
getKeys() {
let current = this._head;
let keys = [];
while (current != null) {
keys.push(current.val.key);
current = current.next;
}
return keys;
}
getValues() {
let current = this._head;
let values = [];
while (current != null) {
values.push(current.val.val);
current = current.next;
}
return values;
}
getPairs() {
let current = this._head;
let pairs = [];
while (current != null) {
pairs.push([current.val.key, current.val.val]);
current = current.next;
}
return pairs;
}
updateValueAt(index, newVal) {
let current = this._head;
let currentIndex = 0;
while (current != null) {
if (currentIndex === index) {
current.val.val = newVal;
return;
}
current = current.next;
currentIndex++;
}
console.warn("Tried to update value at invalid index: " + index);
}
findByKey(key) {
let current = this._head;
let currentIndex = 0;
while (current != null) {
if (current.val.key === key) return currentIndex;
current = current.next;
currentIndex++;
}
// moved through entire list and didn't find target.
return -1;
}
}