-
Notifications
You must be signed in to change notification settings - Fork 0
/
singly-linked-list.js
255 lines (207 loc) · 4.91 KB
/
singly-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
class SLLNode {
constructor(val) {
this.val = val;
this.next = null;
}
}
class SinglyLinkedList {
constructor() {
this.head = this.tail = null;
this.length = 0;
}
push(val) {
if (this.isEmpty()) {
this.head = this.tail = new SLLNode(val);
} else {
this.tail = this.tail.next = new SLLNode(val);
}
this.length++;
return this;
}
unshift(val) {
if (this.isEmpty()) {
this.head = this.tail = new SLLNode(val);
} else {
const newFirstNode = new SLLNode(val);
newFirstNode.next = this.head;
this.head = newFirstNode;
}
this.length++;
return this;
}
pop() {
if (this.isEmpty()) {
return -1;
} else if (this.length === 1) {
const loneNode = this.head;
this.head = this.tail = null;
this.length--;
return loneNode;
} else {
const lastNode = this.tail;
let secondToLast = this.head;
while (secondToLast.next && secondToLast.next.next) {
secondToLast = secondToLast.next;
}
this.tail = secondToLast;
this.length--;
return lastNode;
}
}
shift() {
if (this.isEmpty()) {
return -1;
} else if (this.length === 1) {
const loneNode = this.head;
loneNode.next = null;
this.head = this.tail = null;
this.length--;
return loneNode;
} else {
const firstNode = this.head;
this.head = this.head.next;
firstNode.next = null;
this.length--;
return firstNode;
}
}
get(idx) {
if (!Number.isSafeInteger(idx)) {
throw new Error('Invalid index');
}
// handle invalid idx
// - also handles edge case: empty list since idx = length
if (idx < 0 || idx >= this.length) {
return -1;
}
if (this.isEmpty()) {
return -1;
} else if (this.length === 1) {
return this.head;
} else {
let trav = this.head;
let count = idx;
while (count > 0) {
trav = trav.next;
count--;
}
return trav;
}
}
set(idx, val) {
if (!Number.isSafeInteger(idx)) {
throw new Error('Invalid index');
}
if (idx < 0 || idx >= this.length) {
return -1;
}
const nodeToSet = this.get(idx);
if (nodeToSet) {
nodeToSet.val = val;
} else {
return -1;
}
return this;
}
insert(idx, val) {
if (!Number.isSafeInteger(idx)){
throw new Error('Invalid index');
}
if (idx === 0) {
// O(1) if empty list
// also O(1) if list is not empty
// all u need is a pointer to the head
return this.unshift(val);
}
if (idx < 0 || idx >= this.length) {
return -1;
}
const insertionPoint = this.get(idx - 1);
const newNode = new SLLNode(val);
newNode.next = insertionPoint.next;
insertionPoint.next = newNode;
this.length++;
return this;
}
remove(idx) {
// invalid arg
if (!Number.isSafeInteger(idx)) {
throw new Error('Invalid index');
}
// all invalid indices
if (idx < 0 || idx >= this.length) {
return -1;
}
// removing on either end
if (idx === 0) {
// head
return this.shift();
} else if (idx === this.length - 1) {
// tail
return this.pop();
// removal from any point in middle
} else {
const removalPoint = this.get(idx - 1);
const deleted = removalPoint.next;
removalPoint.next = deleted.next;
this.length--;
deleted.next = null;
return deleted;
}
}
reverse() {
if (this.isEmpty()) {
return -1;
} else if (this.length === 1) {
return this;
} else {
let current = this.head;
// swap in place
[this.head, this.tail] = [this.tail, this.head];
let beforeCurrent = null;
let afterCurrent;
// while(current), not while(current.next)
// - since it must go PAST the tail i.e. only exit when current is null
// - current.next only goes up to tail itself
while (current) {
afterCurrent = current.next; // save a reference to next before modifying link
current.next = beforeCurrent; // fix link
beforeCurrent = current; // move up 1step
current = afterCurrent;
}
return this;
}
}
isEmpty() {
return this.head === null;
}
print() {
const headToTail = [];
if (!this.isEmpty()) {
let trav = this.head;
while (trav !== null) {
headToTail.push(trav.val);
trav = trav.next;
}
}
return headToTail;
}
}
/**
const list = new SinglyLinkedList();
list.push('a');
list.push('b');
list.push('c');
list.push('d')
list.reverse();
const printed = list.print();
console.log(printed[0])
console.log(printed[1])
console.log(printed[2])
console.log(printed[3])
console.log(printed[4])
*/
module.exports = {
SLLNode,
SinglyLinkedList,
}