-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked-list.js
476 lines (436 loc) · 13.6 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
class ListNode {
constructor(data) {
this.data = data;
this.next = null;
}
}
class LinkedList {
constructor(amtToCreate = 0) {
this._head = null;
this.amtToCreate = amtToCreate;
if (this.amtToCreate > 0) {
for (let i = 0; i < this.amtToCreate; ++i) {
this.addToEnd(i);
}
}
}
get head() {
return this._head;
}
set head(node) {
this._head = node;
}
print() {
let str = "";
let returnArray = new Array();
let current = this.head;
while (current) {
str += current.data + ' -> ';
returnArray.push(current.data);
current = current.next;
}
//console.log(`${str.slice(0,-4)}`);
return returnArray;
}
size() {
let current = this.head;
let count = 0;
while (current) {
++count;
current = current.next;
}
return count;
}
getNodeData(nodeIdx) {
if (nodeIdx === 0) {
return this.head.data;
} else {
let current = this.head;
let counter = 0;
while (current) {
if (nodeIdx === counter) {
return current.data;
}
++counter;
current = current.next;
}
if (nodeIdx === counter) {
return current.data;
}
throw new Error('Out of bounds.');
}
}
addToEnd(data) {
const newNode = new ListNode(data);
if (!this.head) {
this.head = newNode;
} else {
let current = this.head;
while (current.next) {
current = current.next;
}
current.next = newNode;
}
}
addToBeginning(data) {
const newNode = new ListNode(data);
if (!this.head) {
this.head = newNode;
} else {
newNode.next = this.head; // Link the new node to the current head
this.head = newNode; // Set the new node as the new head
}
}
remove(data) {
let current = this.head;
let previous = null;
while (current) {
if (current.data === data) {
if (previous) {
previous.next = current.next;
} else {
this.head = current.next;
}
return true; // Data found and removed
}
previous = current;
current = current.next;
}
return false; // Data not found
}
removeAtIdx(idx) {
if (idx > this.size() - 1) {
throw new Error("Out of bounds.");
}
let current = this.head;
let previous = null;
let idxCount = 0;
while (current) {
if (idx === idxCount) {
if (previous) {
previous.next = current.next;
} else {
this.head = current.next;
}
return true; // Data found and removed
}
previous = current;
current = current.next;
++idxCount;
}
}
removeDups() {
let current = this.head;
let previous = null;
let map = new Map();
while (current) {
let isDup = map.get(current.data);
if (isDup === undefined) {
map.set(current.data, true);
previous = current;
} else {
previous.next = current.next;
}
current = current.next;
}
}
nthFromLast(n) {
let length = 0;
let current = this.head;
let behind = this.head;
while (length != n) {
++length;
current = current.next;
if (current === null) {
throw new Error('n out of bounds');
}
}
while (current) {
current = current.next;
if (current === null) {
return behind.data;
}
behind = behind.next;
}
}
deleteMiddleNode(node) {
if (this.head !== null && this.head.next !== null && (this.head.data === node.data && this.head.next.data === node.next.data)) {
throw new Error("Out of bounds.");
}
let current = this.head;
let previous = null;
while (current) {
if (current.next === null) {
throw new Error("Out of bounds.")
}
if (current.data === node.data && current.next.data === node.next.data) {
if (previous) {
previous.next = current.next;
} else {
this.head = current.next;
}
return true; // Data found and removed
}
previous = current;
current = current.next
}
}
partition(node) {
let current = this.head;
let leftTail = new ListNode();
let rightTail = new ListNode();
let rightHead = rightTail;
let leftHead = leftTail;
while (current) {
if (current.data < node.data) {
leftTail.next = current;
leftTail = current;
} else {
rightTail.next = current;
rightTail = current;
}
current = current.next
}
leftTail.next = rightHead.next
rightTail.next = null
this.head = leftHead.next;
}
padList(l2, reverse) {
if (this.size() < l2.size()) {
let zerosToAdd = l2.size() - this.size();
for (let i = 0; i < zerosToAdd; ++i) {
if (reverse) {
this.addToEnd(0);
} else {
this.addToBeginning(0);
}
}
} else if (this.size() > l2.size()) {
let zerosToAdd = this.size() - l2.size();
for (let i = 0; i < zerosToAdd; ++i) {
if (reverse) {
l2.addToEnd(0);
} else {
l2.addToBeginning(0);
}
}
} else {
throw new Error("Lists are of equal size.");
}
}
sumLists(nodesToSum) {
if (nodesToSum === null || this.head === null) {
throw new Error("Source or incoming list cannot be null.");
}
if (this.size() !== nodesToSum.size()) {
this.padList(nodesToSum, true);
}
let linkedListToReturn = new LinkedList();
let current = this.head;
let nodesToSummCurr = nodesToSum.head;
let carry = 0;
while (current || nodesToSummCurr) {
let currentSum = 0;
if (carry > 0) {
currentSum = current.data + nodesToSummCurr.data + carry;
carry = 0;
} else {
currentSum = current.data + nodesToSummCurr.data;
}
if (currentSum > 9) {
carry = Math.floor(currentSum / 10);
let onesValue = currentSum % 10;
linkedListToReturn.addToEnd(onesValue);
} else {
linkedListToReturn.addToEnd(currentSum);
}
current = current.next;
nodesToSummCurr = nodesToSummCurr.next;
}
if (carry > 0) {
linkedListToReturn.addToEnd(carry);
}
return linkedListToReturn.print();
}
doNodeSum(l2, carry) {
let sum = this.getNodeData(this.size() - 1) + l2.getNodeData(l2.size() - 1) + carry;
this.removeAtIdx(this.size() - 1);
l2.removeAtIdx(l2.size() - 1);
if (sum > 9) {
let carry = Math.floor(sum / 10);
let onesValue = sum % 10;
return { carry: carry, onesValue: onesValue };
} else {
return { carry: 0, onesValue: sum };
}
}
sumListsForward(nodesToSum) {
if (nodesToSum === null || this.head === null) {
throw new Error("Source or incoming list cannot be null.");
}
if (this.size() !== nodesToSum.size()) {
this.padList(nodesToSum, false);
}
let linkedListToReturn = new LinkedList();
let carry = 0;
while (this.size() > 0) {
let result = this.doNodeSum(nodesToSum, carry);
linkedListToReturn.addToBeginning(result.onesValue);
carry = result.carry;
}
if (carry > 0) {
linkedListToReturn.addToBeginning(carry);
}
return linkedListToReturn;
}
doNodeSumRecursive(l1, l2) {
if (!l1 && !l2) {
return { node: null, carry: 0 };
}
let result = this.doNodeSumRecursive(
l1 ? l1.next : null, l2 ? l2.next : null);
let sum = result.carry;
if (l1) {
sum += l1.data;
}
if (l2) {
sum += l2.data;
}
let newNode = new ListNode();
newNode.data = sum % 10;
newNode.next = result.node;
return { node: newNode, carry: Math.floor(sum / 10) };
}
sumListsForwardRecursive(nodesToSum) {
if (nodesToSum === null || this.head === null) {
throw new Error("Source or incoming list cannot be null.");
}
if (this.size() !== nodesToSum.size()) {
this.padList(nodesToSum, false);
}
let llToReturn = new LinkedList();
let result = this.doNodeSumRecursive(this.head, nodesToSum.head, 0);
if (result.carry > 0) {
llToReturn.head = new ListNode(result.carry);
llToReturn.head.next = result.node;
} else {
llToReturn.head = result.node;
}
return llToReturn;
}
isPalindrome() {
if (this.size() === 0 || this.size() === 1) {
return true;
}
if (this.getNodeData(0) === this.getNodeData(this.size() - 1)) {
this.removeAtIdx(0);
this.removeAtIdx(this.size() - 1);
return this.isPalindrome();
} else {
return false;
}
}
checkNodeEqualityByRef(n1, n2) {
if (!n1 || !n2) {
return false;
}
return (n1 === n2) ? true : false;
}
getNodeAtIdx(n) {
if (n == null) {
throw new Error("Index cannot be null.");
}
let idxCount = 0;
let current = this.head;
while (current) {
if (idxCount === n) {
return current;
}
current = current.next;
++idxCount;
}
throw new Error("Node not found.");
}
intersection(l2) {
if (!this.head || !l2.head) {
throw new Error("One or both of the lists cannot be null.");
}
if (this.getNodeAtIdx(this.size() - 1) === l2.getNodeAtIdx(l2.size() - 1)) {
let current;
let l2Node;
let sourceOffset = 0;
let inputOffset = 0;
if (this.size() !== l2.size()) {
if (this.size() < l2.size()) {
sourceOffset = l2.size() - this.size();
} else if (this.size() > l2.size()) {
inputOffset = this.size() - l2.size();
}
}
if (sourceOffset === 0 && inputOffset === 0) {
current = this.head;
l2Node = l2.head;
} else if (sourceOffset > 0) {
l2Node = l2.head;
} else if (inputOffset > 0) {
current = this.head;
}
let offsetCount = 0;
while (current || l2Node) {
if (this.checkNodeEqualityByRef(current, l2Node)) {
return current;
}
if (current && l2Node) {
current = current.next;
l2Node = l2Node.next;
} else if (sourceOffset > 0 && offsetCount >= sourceOffset) {
if (!current) {
current = this.head;
} else {
current = current.next;
}
if (l2Node) {
l2Node = l2Node.next;
}
} else if (inputOffset > 0 && offsetCount >= inputOffset) {
if (!l2Node) {
l2Node = l2.head;
} else {
l2Node = l2Node.next;
}
if (current) {
current = current.next;
}
}
++offsetCount;
}
}
throw new Error("Lists do not intersect.");
}
loopDetection() {
if (!this.head) {
throw new Error("List cannot be null.");
}
let slow = this.head;
let fast = this.head;
let loopDetected = false;
while ((!loopDetected && fast && fast.next) || (loopDetected && fast)) {
slow = slow.next;
if (!loopDetected) {
fast = fast.next.next;
} else {
fast = fast.next;
}
if (slow === fast) {
if (!loopDetected) {
loopDetected = true;
slow = this.head;
} else {
return slow;
}
}
}
throw new Error("List does not have a loop.");
}
}
module.exports = { LinkedList, ListNode };