-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverseDoublyLinkedList.js
63 lines (54 loc) · 1.32 KB
/
reverseDoublyLinkedList.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
// Reverse a doubly linked list, in place
class DoublyLinkedListNode {
constructor(val) {
this.val = val;
this.previous = null;
this.next = null;
}
}
class DoublyLinkedList {
constructor() {
this.head = null;
this.tail = null;
this.length = 0;
}
addNode(newVal) {
const node = new DoublyLinkedListNode(newVal);
if (!this.head) {
this.head = node;
this.tail = node;
} else {
node.previous = this.tail;
this.tail.next = node;
this.tail = node;
}
this.length++;
}
reverseList() {
console.log('\nLIST PRIOR TO REVERSING:\n\n', this.head);
let left = this.head;
let right = this.tail;
let current = 0;
while (current < Math.floor(this.length / 2)) {
// swap left and right values, increment left, decrement right
[left.val, right.val] = [right.val, left.val];
left = left.next;
right = right.previous;
current++;
}
return console.log('\nREVERSED LIST:\n\n', this.head);
}
}
const myOddDLL = new DoublyLinkedList();
myOddDLL.addNode(1);
myOddDLL.addNode(2);
myOddDLL.addNode(3);
myOddDLL.addNode(4);
myOddDLL.addNode(5);
myOddDLL.reverseList();
const myEvenDLL = new DoublyLinkedList();
myEvenDLL.addNode(1);
myEvenDLL.addNode(2);
myEvenDLL.addNode(3);
myEvenDLL.addNode(4);
myEvenDLL.reverseList();