-
Notifications
You must be signed in to change notification settings - Fork 0
/
11_linked_list.js
53 lines (44 loc) · 877 Bytes
/
11_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
class LinkedList {
constructor() {
this.root = null;
this.size = 0;
}
add(value) {
if (!this.size) {
this.root = new Node(value);
this.size++;
}
let node = this.root;
while (node.next) {
node = node.next;
}
node.next = new Node(value);
this.size++;
return this;
}
getSize() {
return this.size;
}
print() {
let result = [];
let node = this.root;
while (node) {
result.push(node.value);
node = node.next;
}
console.log(result);
}
}
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
const list = new LinkedList();
list.add(5);
list.add(3);
list.add(2);
list.add(5);
list.add(7);
list.print();