forked from DaviPRocha/hacker-rank-resolutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList.js
47 lines (42 loc) · 868 Bytes
/
LinkedList.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
class LinkedList{
constructor() {
this.head = null
}
remove(i) {
}
insert(val, i) {
const node = {
value: val,
next: null
}
let current = this.head
if (current !== null){
for(let j = 0; j < i; j++) {
current = current.next
}
if (current !== null) {
node.next = current.next
current.next = node
}
} else {
this.head = node
}
}
print() {
let current = this.head
while(current !== null) {
console.log(current.value)
current = current.next
}
console.log('end')
}
}
const x = new LinkedList()
x.insert(2, 0)
x.print()
x.insert(3, 1)
x.print()
x.insert(1, 0)
x.print()
x.insert(4, 3)
x.print()