From e037b8755822d926de2b44eff180824f2baa4b7c Mon Sep 17 00:00:00 2001 From: hailincai Date: Thu, 14 Oct 2021 07:58:33 -0400 Subject: [PATCH] =?UTF-8?q?Update=200206.=E7=BF=BB=E8=BD=AC=E9=93=BE?= =?UTF-8?q?=E8=A1=A8.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加Java从后向前递归的代码 --- problems/0206.翻转链表.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/problems/0206.翻转链表.md b/problems/0206.翻转链表.md index ec6f3dca..3e2a22be 100644 --- a/problems/0206.翻转链表.md +++ b/problems/0206.翻转链表.md @@ -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 #双指针