-
Notifications
You must be signed in to change notification settings - Fork 0
/
Linkedlist.js
349 lines (317 loc) · 7.55 KB
/
Linkedlist.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
// Constructor: LinkedList
// Properties: head, tail
// head: first node of the linked list
// tail: last node of the linked list
// Basic structure of a linked list
/*
const list = {
head: {
value: 6
next: {
value: 10
next: {
value: 12
next: {
value: 3
next: null
}
}
}
}
}
};
*/
// Construct Node class
// Properties: value, next
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
// Construct LinkedList class
// Properties: head, tail, length
class LinkedList {
constructor() {
this.head = null;
this.next = null;
this.length = 0;
}
// Add a new node to the linked list
// Or You can use Add function to add a new node to the linked list
push(value) {
const newNode = new Node(value);
if (!this.head) {
this.head = newNode;
this.tail = this.head;
} else {
this.tail.next = newNode;
this.tail = newNode;
}
this.length++;
}
// Remove the last node from the linked list
pop() {
if (!this.head) return undefined;
let current = this.head;
let newTail = current;
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;
}
// Remove the first node from the linked list
shift() {
if (!this.head) return undefined;
const currentHead = this.head;
this.head = currentHead.next;
this.length--;
if (this.length === 0) {
this.tail = null;
}
return currentHead;
}
// Add a new node to the beginning of the linked list
unshift(value) {
const newNode = new Node(value);
if (!this.head) {
this.head = newNode;
this.tail = this.head;
} else {
newNode.next = this.head;
this.head = newNode;
}
this.length++;
}
// Get the node at a specific position in the linked list
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 the value of a node at a specific position in the linked list
set(index, value) {
const foundNode = this.get(index);
if (foundNode) {
foundNode.value = value;
return true;
}
return false;
}
// Insert a new node at a specific position in the linked list
insert(index, value) {
if (index < 0 || index > this.length) return false;
if (index === this.length) return !!this.push(value);
if (index === 0) return !!this.unshift(value);
const newNode = new Node(value);
const prev = this.get(index - 1);
const temp = prev.next;
prev.next = newNode;
newNode.next = temp;
this.length++;
return true;
}
// Remove a node at a specific position in the linked list
remove(index) {
if (index < 0 || index >= this.length) return undefined;
if (index === 0) return this.shift();
if (index === this.length - 1) return this.pop();
const previousNode = this.get(index - 1);
const removed = previousNode.next;
previousNode.next = removed.next;
this.length--;
return removed;
}
// Reverse the linked list
reverse() {
let node = this.head;
this.head = this.tail;
this.tail = node;
let next;
let prev = null;
for (let i = 0; i < this.length; i++) {
next = node.next;
node.next = prev;
prev = node;
node = next;
}
return this;
}
// Print the linked list
print() {
const arr = [];
let current = this.head;
while (current) {
arr.push(current.value);
current = current.next;
}
console.log(arr);
}
// Traverse the linked list
traverse() {
let current = this.head;
while (current) {
console.log(current.value);
current = current.next;
}
}
// Get the length of the linked list
size() {
return this.length;
}
// Get the first node of the linked list
getFirst() {
return this.head;
}
// Get the last node of the linked list
getLast() {
return this.tail;
}
// Clear the linked list
clear() {
this.head = null;
this.tail = null;
this.length = 0;
}
// Check if the linked list is empty
isEmpty() {
return this.length === 0;
}
// Check if the linked list contains a value
contains(value) {
let current = this.head;
while (current) {
if (current.value === value) {
return true;
}
current = current.next;
}
return false;
}
// Find the index of a value in the linked list
indexOf(value) {
let current = this.head;
let index = 0;
while (current) {
if (current.value === value) {
return index;
}
current = current.next;
index++;
}
return -1;
}
// Find the last index of a value in the linked list
lastIndexOf(value) {
let current = this.head;
let index = -1;
let currentIndex = 0;
while (current) {
if (current.value === value) {
index = currentIndex;
}
current = current.next;
currentIndex++;
}
return index;
}
// Find the middle node of the linked list
middle() {
let slow = this.head;
let fast = this.head;
while (fast && fast.next) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
// Find the nth node from the end of the linked list
nthFromEnd(n) {
let firstPointer = this.head;
let secondPointer = this.head;
for (let i = 0; i < n; i++) {
firstPointer = firstPointer.next;
}
while (firstPointer) {
firstPointer = firstPointer.next;
secondPointer = secondPointer.next;
}
return secondPointer;
}
// Remove duplicates from the linked list
removeDuplicates() {
let current = this.head;
while (current) {
let next = current.next;
while (next) {
if (next.value === current.value) {
current.next = next.next;
}
next = next.next;
}
current = current.next;
}
}
// Remove duplicates from the linked list using a hash table
removeDuplicatesWithHashTable() {
const hashTable = {};
let current = this.head;
let prev = null;
while (current) {
if (hashTable[current.value]) {
prev.next = current.next;
} else {
hashTable[current.value] = true;
prev = current;
}
current = current.next;
}
}
// Find the kth node from the end of the linked list
kthFromEnd(k) {
let current = this.head;
let length = 0;
while (current) {
length++;
current = current.next;
}
if (k > length) return null;
current = this.head;
for (let i = 1; i < length - k + 1; i++) {
current = current.next;
}
return current;
}
}
// Bad way to add a new node to the linked list
// var first = new Node("Hi")
// first.next = new Node("there")
// first.next.next = new Node("how")
// first.next.next.next = new Node("are")
// first.next.next.next.next = new Node("you")
const list = new LinkedList();
list.push(6);
list.push(10);
list.push(3);
list.pop();
console.log(list);
// Time complexity of linked list operations
// Access: O(n)
// Search: O(n)
// Insertion: O(1)
// Deletion: O(1)
// Space complexity: O(n)
// Where n is the number of nodes in the linked list