forked from utimur/algs_and_structures_course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12_linked_list.js
52 lines (45 loc) · 893 Bytes
/
12_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
class LinkedList {
constructor() {
this.size = 0
this.root = null
}
add(value) {
if (this.size === 0) {
this.root = new Node(value);
this.size += 1;
return true;
}
let node = this.root
while (node.next) {
node = node.next
}
let newNode = new Node(value)
node.next = newNode
this.size += 1
}
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()