mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-11 21:10:58 +08:00
Update 0206.翻转链表.md
增加Java从后向前递归的代码
This commit is contained in:
@ -164,6 +164,25 @@ class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
```java
|
||||||
|
// 从后向前递归
|
||||||
|
class Solution {
|
||||||
|
ListNode reverseList(ListNode head) {
|
||||||
|
// 边缘条件判断
|
||||||
|
if(head == null) return null;
|
||||||
|
if (head.next == null) return head;
|
||||||
|
|
||||||
|
// 递归调用,翻转第二个节点开始往后的链表
|
||||||
|
ListNode last = reverseList(head.next);
|
||||||
|
// 翻转头节点与第二个节点的指向
|
||||||
|
head.next.next = head;
|
||||||
|
// 此时的 head 节点为尾节点,next 需要指向 NULL
|
||||||
|
head.next = null;
|
||||||
|
return last;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python迭代法:
|
Python迭代法:
|
||||||
```python
|
```python
|
||||||
#双指针
|
#双指针
|
||||||
|
Reference in New Issue
Block a user