-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path206.py
More file actions
47 lines (35 loc) · 818 Bytes
/
206.py
File metadata and controls
47 lines (35 loc) · 818 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
# 链表逆置
def reverseList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
pre = None
cur = head
while cur:
temp = cur.next
cur.next = pre
pre = cur
cur = temp
return pre
def reverseList1(self, head):
pre = None
while head:
cur = head
head = head.next
cur.next = pre
pre = cur
return pre
if __name__ == "__main__":
head = ListNode(0)
cur = head
for i in range(5):
temp = ListNode(i)
head.next = temp
head = head.next