-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14. Pop in DLL.js
49 lines (45 loc) · 1.02 KB
/
14. Pop in DLL.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
class Node{
constructor(val){
this.val = val;
this.next = null;
this.prev = null;
}
}
class doublyLinkedList{
constructor(){
this.head = null;
this.tail = null;
this.length = 0;
}
push(val){
var newNode = new Node(val);
if(!this.head){
this.head = newNode;
this.tail = newNode;
}else{
this.tail.next = newNode;
newNode.prev = this.tail;
this.tail = newNode;
}
this.length++;
return this;
}
// POP Removes values from the end of the list
pop(){
var newTail = this.tail.prev;
if(!this.head) return undefined;
if(this.length === 1){
this.head = null;
this.tail = null;
}
this.tail = newTail;
this.tail.next = null;
this.length--;
return this;
}
}
var list = new doublyLinkedList();
list.push("hi");
list.push("I am");
list.push("arsh");
list.push("meharwal")