forked from rohan-paul/Awesome-JavaScript-Interviews
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked-list.js
93 lines (71 loc) · 2.56 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
function LinkedList() {
this.head = null;
this.tail = null;
}
function Node(value, next, prev) {
this.value = value;
this.next = next;
this.prev = prev;
}
//Adding Head Node;
LinkedList.prototype.addToHead = function (value) {
var newNode = new Node(value, this.head, null);
// If head is present add new node to head.prev
if (this.head) {
this.head.prev = newNode;
} else {
this.tail = newNode;
} //else add new node to head, because no head means no node in linked list
// Irrespective of whether head is present or not, we need to add newNode as head.
this.head = newNode;
}
LinkedList.prototype.addToTail = function (value) {
var newNode = new Node(value, this.head, null);
if (this.tail) {
this.tail.next = newNode; // if tail is present, then add newNode to be tail.next
} else {
this.head = newNode; // else add new node to head, becuse no tail means no node at all in the linked-list
}
this.tail = newNode; // Irrespective of whether tail is present or not, we need to add newNode as tail which is the ultimate purpose of this function.
}
LinkedList.prototype.removeHead = function () {
if (!this.head) return null; // No head at all means its an empty list
// first assign a variable (handler) with the head value
let value = this.head.value;
this.head = this.head.next; // Then make the next node to be the head, as I will be removing the head.
if (this.head) {
this.head.prev = null; //If head is present (means head.next) remove head.prev value
} else {
this.tail = null; //No head means no tail (empty list)
}
return value;
}
LinkedList.prototype.removeTail = function () {
if (!this.tail) return null; //No tail means empty list
// first assign a variable with the head value
let value = this.tail.value;
this.tail = this.tail.prev; //Move tail to tail.prev as I will be removing this tail
if (this.tail) {
this.tail.next = null // If tail is present (means tail.prev) remove tail.next value
} else {
this.head = null; //No tail means no head (empty list)
}
return value;
}
LinkedList.prototype.search = function (searchValue) {
let currentNode = this.head;
while (currentNode) {
if (currentNode.value == searchValue) {
return currentNode.value;
}
currentNode = currentNode.next;
}
};
ll = new LinkedList();
ll.addToHead(300);
ll.addToHead(400);
ll.addToTail(200);
ll.addToTail(100);
// ll.removeHead();
// ll.removeTail();
console.log(ll);