-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedList.js
74 lines (58 loc) · 1.48 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
//* In this file I will implement the linkedin with some common getter setter function.
class LinkedList {
constructor() {
this.nodes = [];
}
// get side function
get size() {
return this.nodes.length;
}
// get the head
get head() {
return this.size ? this.nodes[0] : null;
}
// get the last element
get tail() {
return this.size ? this.nodes[this.size - 1] : null;
}
insertAt(index, value) {
let previousNode = this.nodes[index - 1] || null;
let nextNode = this.nodes[index] || null;
let node = { value, next: nextNode };
if (previousNode) previousNode.next = node;
this.nodes.splice(index, 0, node);
}
insertFirst(value) {
return this.insertAt(0, value);
}
insertLast(value) {
return this.insertAt(this.size, value);
}
getAt(index) {
return this.nodes[index];
}
removeAt(index) {
let previousNode = this.nodes[index - 1];
let nextNode = this.nodes[index + 1] || null;
if (previousNode) previousNode.next = nextNode;
return this.nodes.splice(index, 1);
}
reverse() {
this.nodes = this.nodes.reduce(
(acc, { value }) => [{ value, next: acc[0] || null }, ...acc],
[]
);
}
clear() {
this.nodes = [];
}
}
const linkedList = new LinkedList();
linkedList.insertFirst(5);
linkedList.insertFirst(6);
linkedList.insertFirst(7);
// linkedList.removeAt(0);
linkedList.insertAt(1, 10);
// console.log(linkedList.reverse());
linkedList.reverse();
console.log(linkedList.head);