-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_list.js
More file actions
44 lines (43 loc) Β· 981 Bytes
/
linked_list.js
File metadata and controls
44 lines (43 loc) Β· 981 Bytes
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
// Hazrat Ali
// University Of Scholars
class Node {
constructor(value){
this.value = value;
this.next = null;
this.previous = null;
}
}
// linked list
class LinkedList {
constructor(head){
this.head = head;
}
add(value){
const newNode = new Node(value);
let current = this.head;
while(current.next != null){
current = current.next;
}
current.next = newNode;
}
remove(value) {
let current = this.head;
let previous = null;
while (current !== null) {
if(current.value === value){
previous.next = current.next;
break;
}
previous = current;
current = current.next;
}
}
}
const head = new Node(1500);
const mBondon = new LinkedList(head);
mBondon.add(27)
mBondon.add(13)
mBondon.add(59)
mBondon.add(68)
mBondon.add(43)
console.log(JSON.stringify(mBondon))