From fed1fffac571444cdbf5868ef986a4d627e577ce Mon Sep 17 00:00:00 2001 From: roylx <73628821+roylx@users.noreply.github.com> Date: Fri, 27 Jan 2023 09:40:18 -0700 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 Python递归法从后向前 --- problems/0206.翻转链表.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/problems/0206.翻转链表.md b/problems/0206.翻转链表.md index 7db80fe1..d29090d9 100644 --- a/problems/0206.翻转链表.md +++ b/problems/0206.翻转链表.md @@ -228,7 +228,22 @@ class Solution: ``` +Python递归法从后向前: +```python +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: + if not head or not head.next: return head + p = self.reverseList(head.next) + head.next.next = head + head.next = None + return p +``` Go: