-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16. Shift in DLL.js
66 lines (60 loc) · 1.36 KB
/
16. Shift 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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(){
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;
}
shift(){
if(!this.head){
return false;
}
if(this.length===1){
this.head = null;
this.tail = null;
}else{
var oldHead = this.head;
var newHead = this.head.next;
this.head = newHead;
this.head.prev = null;
this.length--;
return this;
}
}
}
var list = new doublyLinkedList();
list.push("hi");
list.push("I am");
list.push("arsh");
list.push("meharwal")