Update 0206.翻转链表.md

增加Java从后向前递归的代码
This commit is contained in:
hailincai 2021-10-14 07:58:33 -04:00 committed by GitHub
parent aedda8732e
commit e037b87558
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 19 additions and 0 deletions

View File

@ -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
#双指针