update反转链表:添加python递归法的代码

This commit is contained in:
Yan Wen
2021-06-21 17:07:27 +08:00
parent fb21a5727e
commit 7d3b01b3b0

View File

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