-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSinglyLinkedList.js
141 lines (120 loc) · 2.88 KB
/
SinglyLinkedList.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
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class SinglyLinkedList {
constructor() {
this.head = null;
this.tail = null;
this.length = 0;
}
push(value) {
let node = new Node(value);
if (!this.length) this.head = this.tail = node;
else {
this.tail.next = node;
this.tail = node;
}
this.length++;
return this;
}
pop() {
if (!this.length) return;
let current = this.head;
let prev = current;
if (this.length === 1) {
this.head = this.tail = null;
} else {
while (current.next) {
prev = current;
current = current.next;
}
this.tail = prev;
this.tail.next = null;
}
this.length--;
return current;
}
get(index) {
if (index < 0 || index >= this.length) return undefined;
let current = this.head;
for (let i = 0; i < this.length; i++) {
if (i === index) break;
current = current.next;
}
return current;
}
set(index, value) {
let node = this.get(index);
if (!node) return false;
node.value = value;
return true;
}
unshift(value) {
if (this.length === 0) this.push(value);
else {
let node = new Node(value);
node.next = this.head;
this.head = node;
}
return this;
}
shift() {
if (this.length === 0) return;
if (this.length === 1) return this.pop();
let removed = this.head;
this.head = removed.next;
removed.next = null;
return removed;
}
insert(index, value) {
if (index < 0 || index > this.length) return false;
if (index === 0) return !!this.unshift(value);
else if (index === this.length) return !!this.push(value);
else {
let node = new Node(value);
let left = this.get(index - 1);
let right = this.get(index);
left.next = node;
node.next = right;
this.length++;
return true;
}
}
remove(index) {
if (index < 0 || index >= this.length) return;
if (index === 0) return this.shift();
else if (index === this.length - 1) this.pop();
else {
let left = this.get(index - 1);
let removed = left.next;
let right = removed.next;
left.next = right;
this.length--;
return removed;
}
}
}
let list = new SinglyLinkedList();
list.push(1); // 1
list.push(2); // 1 -> 2
list.push(5); // 1 -> 2 -> 5
list.push(13); // 1 -> 2 -> 5 -> 13
list.push(12); // 1 -> 2 -> 5 -> 13 -> 12
list.push(500); // 1 -> 2 -> 5 -> 13 -> 12 -> 500
list.pop(); // 1 -> 2 -> 5 -> 13 -> 12
list.pop(); // 1 -> 2 -> 5 -> 13
console.log(list.get(0).value); // 1
console.log(list.get(1).value); // 2
console.log(list.get(2).value); // 5
console.log(list.set(2, "Change 1")); // true
console.log(list.set(100, "Change 1")); // false
list.unshift("Unshifted in 1");
list.unshift("Unshifted in 2");
console.log(list.shift().value); // Unshifted in 2
console.log(list.insert(2, "Inserted")); // true
console.log(list.insert(5, "Inserted to the end")); // true
console.log(list.insert(200, "Inserted to invalid index")); // false
console.log(list);