Skip to content

Commit 5e18608

Browse files
committed
添加 0206.翻转链表 python3版本
1 parent 3389f67 commit 5e18608

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

problems/0206.翻转链表.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,25 @@ class Solution {
143143
```
144144

145145
Python:
146-
146+
```python
147+
#双指针
148+
# Definition for singly-linked list.
149+
# class ListNode:
150+
# def __init__(self, val=0, next=None):
151+
# self.val = val
152+
# self.next = next
153+
class Solution:
154+
def reverseList(self, head: ListNode) -> ListNode:
155+
cur = head
156+
pre = None
157+
while(cur!=None):
158+
temp = cur.next # 保存一下 cur的下一个节点,因为接下来要改变cur->next
159+
cur.next = pre #反转
160+
#更新pre、cur指针
161+
pre = cur
162+
cur = temp
163+
return pre
164+
```
147165

148166
Go:
149167

0 commit comments

Comments
 (0)