Skip to content

Commit e037b87

Browse files
authored
Update 0206.翻转链表.md
增加Java从后向前递归的代码
1 parent aedda87 commit e037b87

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

problems/0206.翻转链表.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,25 @@ class Solution {
164164
}
165165
```
166166

167+
```java
168+
// 从后向前递归
169+
class Solution {
170+
ListNode reverseList(ListNode head) {
171+
// 边缘条件判断
172+
if(head == null) return null;
173+
if (head.next == null) return head;
174+
175+
// 递归调用,翻转第二个节点开始往后的链表
176+
ListNode last = reverseList(head.next);
177+
// 翻转头节点与第二个节点的指向
178+
head.next.next = head;
179+
// 此时的 head 节点为尾节点,next 需要指向 NULL
180+
head.next = null;
181+
return last;
182+
}
183+
}
184+
```
185+
167186
Python迭代法:
168187
```python
169188
#双指针

0 commit comments

Comments
 (0)