1// O(n) time & O(n) space
2function reverse(head) {
3 if (!head || !head.next) {
4 return head;
5 }
6 let tmp = reverse(head.next);
7 head.next.next = head;
8 head.next = undefined;
9 return tmp;
10}
11
1function LinkedListNode(value) {
2 this.value = value;
3 this.next = null;
4}
5let head = new LinkedListNode(10)
6head.next = new LinkedListNode(25)
7head.next.next = new LinkedListNode(46)
8
9// Recursive
10const reverse = (head) => {
11 if (!head || !head.next) {
12 return head;
13 }
14 let temp = reverse(head.next);
15 head.next.next = head;
16 head.next = undefined;
17 return temp;
18}
19head = reverse(head)