-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path206_Reverse_Linked_List.py
70 lines (55 loc) · 1.44 KB
/
206_Reverse_Linked_List.py
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""
Reverse a singly linked list.
Example:
Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL
Follow up:
A linked list can be reversed either iteratively or recursively. Could you implement both?
"""
# 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
"""
"""
Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL
newH =
#方法1,利用指针,ref:https://leetcode.com/problems/reverse-linked-list/discuss/140916/Python-Iterative-and-Recursive
1->2->3->4->5->None
先构造 1->None
然后当前的元素cur作为下一轮的前缀元素 pre = cur
所以得到了 2-> 1->None
当cur=5时,pre = cur = 5,再一次的while循环就进不去了,此时链表头就是 pre的值,返回pre
"""
pre = None
while head:
cur = head
head = head.next
cur.next = pre
pre = cur
return pre
"""
方法2:递归法,
"""
def reverseList_recurssive(self, head):
pass
so = Solution()
p1 = ListNode(1)
p2 = ListNode(2)
p3 = ListNode(3)
p4 = ListNode(4)
p5 = ListNode(5)
head = p1
p1.next = p2
p2.next = p3
p3.next = p4
p4.next = p5
p5.next = None
print(so.reverseList(head))