Update 0206.翻转链表.md

Python递归法从后向前
This commit is contained in:
roylx 2023-01-27 09:40:18 -07:00 committed by GitHub
parent 4f7e3d969e
commit fed1fffac5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 0 deletions

View File

@ -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