From 7d3b01b3b06bf5b898225478c245304a628f1cfb Mon Sep 17 00:00:00 2001 From: Yan Wen Date: Mon, 21 Jun 2021 17:07:27 +0800 Subject: [PATCH] =?UTF-8?q?update=E5=8F=8D=E8=BD=AC=E9=93=BE=E8=A1=A8?= =?UTF-8?q?=EF=BC=9A=E6=B7=BB=E5=8A=A0python=E9=80=92=E5=BD=92=E6=B3=95?= =?UTF-8?q?=E7=9A=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0206.翻转链表.md | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/problems/0206.翻转链表.md b/problems/0206.翻转链表.md index 7c002382..963d7916 100644 --- a/problems/0206.翻转链表.md +++ b/problems/0206.翻转链表.md @@ -142,7 +142,7 @@ class Solution { } ``` -Python: +Python迭代法: ```python #双指针 # Definition for singly-linked list. @@ -163,6 +163,32 @@ class Solution: return pre ``` +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: ListNode) -> ListNode: + + def reverse(pre,cur): + if not cur: + return pre + + tmp = cur.next + cur.next = pre + + return reverse(cur,tmp) + + return reverse(None,head) + +``` + + + Go: ```go