Skip to content

Commit eb2596c

Browse files
committed
reverse linked list solution
- using recursive
1 parent 643ea3e commit eb2596c

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

reverse-linked-list/hyer0705.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* }
1111
*/
1212

13+
// using iterative
1314
function reverseList(head: ListNode | null): ListNode | null {
1415
if (!head) return null;
1516

@@ -25,3 +26,15 @@ function reverseList(head: ListNode | null): ListNode | null {
2526

2627
return prev;
2728
}
29+
30+
// using recursive
31+
function reverseList(head: ListNode | null): ListNode | null {
32+
if (!head || !head.next) return head;
33+
34+
const newHead = reverseList(head.next);
35+
36+
head.next.next = head;
37+
head.next = null;
38+
39+
return newHead;
40+
}

0 commit comments

Comments
 (0)