We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 643ea3e commit eb2596cCopy full SHA for eb2596c
reverse-linked-list/hyer0705.ts
@@ -10,6 +10,7 @@
10
* }
11
*/
12
13
+// using iterative
14
function reverseList(head: ListNode | null): ListNode | null {
15
if (!head) return null;
16
@@ -25,3 +26,15 @@ function reverseList(head: ListNode | null): ListNode | null {
25
26
27
return prev;
28
}
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