-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked-list.js
101 lines (84 loc) · 2.2 KB
/
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//https://medium.freecodecamp.com/a-gentle-introduction-to-data-structures-how-linked-lists-work-5adc793897dd#.uhn4yco0p
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class LinkedList {
constructor() {
this._head = null;
this._tail = null;
this._length = 0;
}
add(value) {
let node = new Node(value);
if(this._head == null && this._tail == null) {
this._head = node;
this._tail = node;
} else {
this._tail.next = node;
this._tail = node;
}
this._length++;
}
contains(value) {
let node = this._head;
while(node) {
if(node.value === value) {
return true;
}
node = node.next;
}
return false;
}
remove(value) {
if(this.contains(value)){
let current = this._head;
let previous = current;
while(current) {
if(current.value === value){
if(this._head === current) {
this._head = this._head.next;
this._length--;
return;
}
if(this._tail === current){
this._tail = previous;
}
previous.next = current.next;
this._length--;
return;
}
previous = current;
current = current.next;
}
}
}
size() {
return this._length;
}
}
/*const AmazingRace = new LinkedList();
console.log('size:', AmazingRace.size());
AmazingRace.add("Colombo, Sri Lanka");
AmazingRace.add("Lagos, Nigeria");
AmazingRace.add("Surat, India");
AmazingRace.add("Suzhou, China");
console.log('size:', AmazingRace.size());
//Kent's check
console.log(AmazingRace.contains('Suzhou, China')); //true
console.log(AmazingRace.contains('Hanoi, Vietnam')); //false
AmazingRace.add('Hanoi, Vietnam');
console.log('size:', AmazingRace.size());
console.log(AmazingRace.contains('Seattle, Washington')); //false
AmazingRace.add('Seattle, Washington');
console.log('size:', AmazingRace.size());
console.log(AmazingRace.contains('North Pole')); // false
AmazingRace.add('North Pole');
console.log('size:', AmazingRace.size());
AmazingRace.remove('North Pole');
console.log('size:', AmazingRace.size());*/
//add : O(1)
//remove : O(n)
//contains : O(n)