添加 0206.翻转链表 python3版本
This commit is contained in:
parent
3389f6799c
commit
5e1860876b
|
|
@ -143,7 +143,25 @@ 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: ListNode) -> ListNode:
|
||||
cur = head
|
||||
pre = None
|
||||
while(cur!=None):
|
||||
temp = cur.next # 保存一下 cur的下一个节点,因为接下来要改变cur->next
|
||||
cur.next = pre #反转
|
||||
#更新pre、cur指针
|
||||
pre = cur
|
||||
cur = temp
|
||||
return pre
|
||||
```
|
||||
|
||||
Go:
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue