-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlinkedList.html
More file actions
73 lines (62 loc) · 1.51 KB
/
linkedList.html
File metadata and controls
73 lines (62 loc) · 1.51 KB
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
<script>
class LinkedList {
constructor() {
this.length = 0;
this.head = null;
}
insert(index, value) {
if (index < 0 || index > this.length) {
throw new Error('Index error');
}
const newNode = {
value
};
if (index == 0) {
newNode.next = this.head;
this.head = newNode;
}
else {
// Find the node which we want to insert after
const node = this._find(index - 1);
newNode.next = node.next;
node.next = newNode;
}
this.length++;
}
_find(index) {
let node = this.head;
for (let i=0; i<index; i++) {
node = node.next;
}
return node;
}
get(index) {
if (index < 0 || index >= this.length) {
throw new Error('Index error');
}
return this._find(index).value;
}
_findMid() {
var fastPointer = this.head;
var slowPointer = this.head;
// loop through the linked list
// when fastPointer reaches the end of the list
// then slowPointer will be at the middle node
while (fastPointer.next !== null && fastPointer.next.next !== null) {
// console.log(slowPointer);
fastPointer = fastPointer.next.next;
slowPointer = slowPointer.next;
}
console.log(slowPointer);
return slowPointer.value;
}
}
let l = new LinkedList;
l.insert(0,'superman');
l.insert(1,'batman');
l.insert(2,'birdman');
l.insert(3,'catwoman');
l.insert(4,'black panther');
l.insert(5,'the flash');
l.insert(6,'the joker');
</script>