-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreverseDLL.js
81 lines (73 loc) · 2.04 KB
/
reverseDLL.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
/*
reverse
This function should reverse all of the nodes in a DoublyLinkedList, and should return the list.
let doublyLinkedList = new DoublyLinkedList;
doublyLinkedList.push(5).push(10).push(15).push(20)
doublyLinkedList.reverse(); // singlyLinkedList;
doublyLinkedList.length; // 4
doublyLinkedList.head.val); // 20
doublyLinkedList.head.next.val; // 15
doublyLinkedList.head.next.next.val; // 10
doublyLinkedList.head.next.next.next.val; // 5
*/
class Node {
constructor(val){
this.val = val;
this.prev = null;
this.next = null;
}
}
class DoublyLinkedList {
constructor(){
this.head = null;
this.tail = null;
this.length = 0;
}
push(val) {
var node = new Node(val);
if (this.head === null) {
this.head = node;
this.tail = this.head;
this.length++;
} else {
this.tail.next = node;
node.prev = this.tail;
this.tail = node;
this.length++;
}
return this;
}
reverse(){
if(this.length === 0){
return null;
}
if (this.length === 1) {
return this;
}
else if(this.length > 1){
let currNode = this.head;
let prevNode = null;
let nextNode = null;
for(let i = 0; i < this.length; i++){
prevNode = currNode.prev;
nextNode = currNode.next;
if(prevNode === null){
this.tail = currNode;
currNode.next = null;
currNode.prev = nextNode;
}
else if(nextNode === null){
this.head = currNode;
currNode.prev = null;
currNode.next = prevNode;
}
else{
currNode.next = prevNode;
currNode.prev = nextNode;
}
currNode = nextNode;
}
return this;
}
}
}