Skip to content

Commit 771f603

Browse files
committed
⚡ [206] Recursive solution
1 parent 5d21aff commit 771f603

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

206/o_solution.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class ListNode {
2+
constructor(val, next) {
3+
this.val = (val === undefined ? 0 : val);
4+
this.next = (next === undefined ? null : next);
5+
}
6+
}
7+
var reverseList = function (head) {
8+
// Special case...
9+
if (head == null || head.next == null) return head;
10+
// Create a new node to call the function recursively and we get the reverse linked list...
11+
var res = reverseList(head.next);
12+
// Set head node as head.next.next...
13+
head.next.next = head;
14+
//set head's next to be null...
15+
head.next = null;
16+
17+
console.log("res")
18+
console.log(res)
19+
return res; // Return the reverse linked list...
20+
};
21+
22+
reverseList(new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5))))))

0 commit comments

Comments
 (0)