-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkList.js
More file actions
42 lines (35 loc) · 702 Bytes
/
linkList.js
File metadata and controls
42 lines (35 loc) · 702 Bytes
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
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class LinkedList {
constructor() {
this.head = null;
}
// Add a value at beginning of list
addStart(value) {
const node = new Node(value);
node.next = this.head;
this.head = node;
}
// Add a value at end of list
addEnd(value) {
const node = new Node(value);
let curr = this.head;
if (curr == null) {
this.head = node;
return;
}
while (curr !== null && curr.next !== null) {
curr = curr.next;
}
curr.next = node;
}
}
const list = new LinkedList();
list.addStart(1);
list.addStart(2);
list.addEnd(3);
console.log(list.head.value);