Skip to content

Commit

Permalink
Merge pull request youngyangyang04#1879 from roylx/master
Browse files Browse the repository at this point in the history
206 增加Python递归法从后向前
  • Loading branch information
youngyangyang04 authored Feb 2, 2023
2 parents 4d262c7 + 041a2a8 commit 4afb0ac
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions problems/0206.翻转链表.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down

0 comments on commit 4afb0ac

Please sign in to comment.