Skip to content

Commit fed1fff

Browse files
authored
Update 0206.翻转链表.md
Python递归法从后向前
1 parent 4f7e3d9 commit fed1fff

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

problems/0206.翻转链表.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,22 @@ class Solution:
228228

229229
```
230230

231+
Python递归法从后向前:
231232

233+
```python
234+
# Definition for singly-linked list.
235+
# class ListNode:
236+
# def __init__(self, val=0, next=None):
237+
# self.val = val
238+
# self.next = next
239+
class Solution:
240+
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
241+
if not head or not head.next: return head
242+
p = self.reverseList(head.next)
243+
head.next.next = head
244+
head.next = None
245+
return p
246+
```
232247

233248
Go:
234249

0 commit comments

Comments
 (0)