Merge pull request #1879 from roylx/master

206 增加Python递归法从后向前
This commit is contained in:
程序员Carl
2023-02-02 09:59:24 +08:00
committed by GitHub

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 Go