mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Merge pull request #426 from borninfreedom/master
update反转链表:添加python递归法的代码
This commit is contained in:
@ -142,7 +142,7 @@ class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Python:
|
Python迭代法:
|
||||||
```python
|
```python
|
||||||
#双指针
|
#双指针
|
||||||
# Definition for singly-linked list.
|
# Definition for singly-linked list.
|
||||||
@ -163,6 +163,32 @@ class Solution:
|
|||||||
return pre
|
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:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
Reference in New Issue
Block a user