Skip to content

Commit 2bd71c4

Browse files
author
Li Li
committed
add recursion version to 206
1 parent 64a6fff commit 2bd71c4

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

Algorithms Basics/Chapter 2. Linked List/206. Reverse Linked List.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// iteration
12
public class Solution {
23
public ListNode ReverseList(ListNode head) {
34
ListNode pre = null, temp = null;
@@ -9,4 +10,15 @@ public ListNode ReverseList(ListNode head) {
910
}
1011
return pre;
1112
}
13+
}
14+
15+
// recursion
16+
public class Solution {
17+
public ListNode ReverseList(ListNode head) {
18+
if (head == null || head.next == null) return head;
19+
ListNode newHead = ReverseList(head.next);
20+
head.next.next = head;
21+
head.next = null;
22+
return newHead;
23+
}
1224
}

0 commit comments

Comments
 (0)