-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathreverse-doubly-linked-list.js
More file actions
56 lines (46 loc) · 1.01 KB
/
reverse-doubly-linked-list.js
File metadata and controls
56 lines (46 loc) · 1.01 KB
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
'use strict';
/*
Title: Reverse a doubly linked list
Difficulty: Easy
Score: 5
Link: https://www.hackerrank.com/challenges/reverse-a-doubly-linked-list/problem
*/
function reverse(head) {
while (head.next) {
head = head.next
}
const newList = new DoublyLinkedList();
while (head) {
newList.insertNode(head.data);
head = head.prev
}
return newList.head
}
class DoublyLinkedListNode {
constructor(nodeData) {
this.data = nodeData;
this.next = null;
this.prev = null;
}
};
class DoublyLinkedList {
constructor() {
this.head = null;
this.tail = null;
}
insertNode(nodeData) {
let node = new DoublyLinkedListNode(nodeData);
if (this.head == null) {
this.head = node;
} else {
this.tail.next = node;
node.prev = this.tail;
}
this.tail = node;
}
};
module.exports = {
reverse,
DoublyLinkedList,
DoublyLinkedListNode
}