-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSinglyLinkedList.js
More file actions
337 lines (305 loc) · 7.72 KB
/
Copy pathSinglyLinkedList.js
File metadata and controls
337 lines (305 loc) · 7.72 KB
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
class Node {
constructor(val) {
this.val = val;
this.next = null;
}
}
class SinglyLinkedList {
constructor() {
this.head = null;
this.tail = null;
this.length = 0;
}
push(val) {
const newNode = new Node(val);
if (!this.head) {
this.head = newNode;
this.tail = this.head;
} else {
this.tail.next = newNode;
this.tail = newNode;
}
this.length++;
return this;
}
// 1 -> 2 -> 3 -> 4
// nt
// c
pop() {
if (!this.head) return undefined;
let current = this.head;
let newTail = this.head;
while (current.next) {
newTail = current;
current = current.next;
}
this.tail = newTail;
this.tail.next = null
this.length--;
if (this.length === 0) {
this.head = null;
this.tail = null;
}
return current;
}
shift() {
if (!this.head) return undefined;
let currentHead = this.head;
this.head = currentHead.next;
this.length--;
if (this.length === 0) {
this.tail = null;
}
return currentHead;
}
unshift(val) {
const newNode = new Node(val);
if (!this.head) {
this.head = newNode;
this.tail = newNode;
} else {
let prevNode = this.head;
this.head = newNode;
this.head.next = prevNode;
}
this.length++;
return this;
}
get(index) {
if (index < 0 || index >= this.length) return null;
let counter = 0;
let current = this.head;
while (counter !== index) {
current = current.next;
counter++;
}
return current;
}
set(val, index) {
const foundNode = this.get(index);
if (foundNode) {
foundNode.val = val;
return true;
}
return false;
}
// 0 -> 1 -> 2
// 0.5
insert(val, index) {
if (index < 0 || index > this.length) return false;
if (index === 0) return !!this.unshift(val);
if (index === this.length) return !!this.push(val);
const newNode = new Node(val);
let prevNode = this.get(index - 1);
let temp = prevNode.next;
prevNode.next = newNode;
newNode.next = temp;
this.length++;
return true;
}
remove(index) {
if (index < 0 || index > this.length) return false;
if (index === 0) return !!this.shift();
if (index === this.length) return !!this.pop();
const prevNode = this.get(index - 1);
let removed = prevNode.next;
prevNode.next = removed.next;
this.length--;
return true;
}
reverse() {
let node = this.head;
this.head = this.tail;
this.tail = node;
let next;
let prev = null;
let i = 0;
while (i < this.length) {
next = node.next;
node.next = prev;
prev = node;
node = next;
i++;
}
return this;
}
print() {
const data = [];
let current = this.head;
while (current) {
data.push(current.val)
current = current.next;
}
return data;
}
middleElement() {
let i = this.head;
let j = this.head;
while (j !== null && j.next !== null) {
i = i.next;
j = j.next.next;
}
return i.val;
}
detectCycle() {
let i = this.head;
let j = this.head;
while (j !== null && j.next !== null) {
i = i.next;
j = j.next.next;
if (i === j) {
return true;
}
}
return false;
}
getDetectedCycleValue() {
let i = this.head;
let j = this.head;
while (j !== null && j.next !== null) {
i = i.next;
j = j.next.next;
if (i === j) {
return i;
}
}
return null;
}
detectCycleNode() {
let meet = this.getDetectedCycleValue();
if (meet === null) return 0;
let current = this.head;
while (current.val !== meet.val) {
current = current.next;
meet = meet.next;
}
return current;
}
//Find length of Loop
countNodesinLoop() {
let meet = this.getDetectedCycleValue()
if (meet === null) return 0;
let current = meet;
let count = 1;
while (current.next != meet) {
count++;
current = current.next;
}
return count;
}
// remove duplictes from sorted Linked List
removeDuplicates() {
let current = this.head;
while (current.next) {
if (current.data === current.next.data) {
current.next = current.next.next;
} else {
current = current.next;
}
}
return this.head;
}
//Nth node from end of linked list
getNthFromLast(n) {
let i = 1;
let current = this.head;
while (current.next) {
current = current.next;
i++;
}
if (i < n) {
return -1;
} else {
let el = this.get(head, i - n)
return el.data;
}
}
//Remove duplicates from an unsorted linked list
removeDuplicates() {
let current = this.head;
let hashMap = {}
while (current.next) {
hashMap[current.data] = 1;
if (current.next && hashMap[current.next.data]) {
current.next = current.next.next;
} else {
current = current.next;
}
}
return this.head;
}
//Delete Middle of Linked List
deleteMiddle(node) {
let i = 1;
let current = this.head;
while (current.next) {
current = current.next;
i++;
}
let mid = ~~(i / 2);
if (i & 1 !== 0) {
mid + 1
}
let prevNode = this.get(mid - 1, this.head);
let removed = prevNode.next;
prevNode.next = removed.next;
return node;
}
//Remove loop
removeLoop() {
let slow = this.head;
let fast = this.head;
let prev;
while (fast != null && fast.next != null) {
prev = slow;
slow = slow.next;
fast = fast.next.next;
if (slow == fast) {
break;
}
}
if (fast == null || fast.next == null) {
return;
}
let tmp = this.head;
while (tmp != slow) {
tmp = tmp.next;
prev = slow;
slow = slow.next;
}
prev.next = null;
return this.head;
}
// Pair wise swap
// Input = [1, 2, 3, 4]
// Output = [2, 1, 4, 3]
pairWiseSwap(node) {
let current = node;
while (current != null && current.next != null) {
let val = current.data;
current.data = current.next.data;
current.next.data = val;
current = current.next.next;
}
return node;
}
}
list = new SinglyLinkedList();
list.push(1);
list.push(2);
list.push(3);
list.push(4);
list.push(5);
// console.log(list)
// console.log(list.pop());
// console.log(list.shift());
//list.unshift(0);
//list.get(1);
//list.set(10, 0);
//list.insert(1, 1);
//list.remove(1);
console.log(list);
console.log(list.print());
console.log(list.middleElement());
//list.reverse();
//console.log(list.print());