Skip to content

Commit 119029a

Browse files
committed
reverse list
1 parent 9fcfed9 commit 119029a

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Node {
2+
constructor(data) {
3+
this.data = data;
4+
this.next = null;
5+
}
6+
}
7+
8+
function reverseList(list) {
9+
let currentNode = null,
10+
nextNode = null;
11+
while (list != null) {
12+
nextNode = list.next;
13+
list.next = currentNode;
14+
currentNode = list;
15+
list = nextNode;
16+
}
17+
return currentNode;
18+
}
19+
20+
function displayList(list) {
21+
let output = ''
22+
while (list != null) {
23+
output += list.data + ' ';
24+
list = list.next;
25+
}
26+
console.log(output);
27+
}
28+
29+
let list = new Node(1);
30+
list.next = new Node(2);
31+
list.next.next = new Node(3);
32+
list.next.next.next = new Node(4);
33+
list.next.next.next.next = new Node(5);
34+
list.next.next.next.next.next = new Node(6);
35+
let head = reverseList(list);
36+
displayList(head);

0 commit comments

Comments
 (0)