forked from algorhythms/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
092 Reverse Linked List II.py
90 lines (72 loc) · 2.18 KB
/
092 Reverse Linked List II.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"""
Reverse a linked list from position m to n. Do it in-place and in one-pass.
For example:
Given 1->2->3->4->5->NULL, m = 2 and n = 4,
return 1->4->3->2->5->NULL.
Note:
Given m, n satisfy the following condition:
1 <= m <= n <= length of list.
"""
__author__ = 'Danyang'
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
def __repr__(self):
return repr(self.val)
def __str__(self):
return str(self.val)+", "+str(self.next)
class Solution:
def reverseBetween(self, head, m, n):
"""
Linked List
[m, n)
:param head: ListNode
:param m: int
:param n: int
:return: ListNode
"""
# trivial
if not head or m>=n:
return head
dummy = ListNode(0)
dummy.next = head
cnt = 1 # position starting from 1
pre = dummy
start_pre = None
start = None
cur = pre.next # cannot put it in while loop? affect reverse link
while pre.next:
# record starting point
if cnt==m:
start_pre = pre
start = cur
# reverse link (not node)
# 1 -> 2 -> 3
# 1 <- 2 -> 3
elif m<cnt<=n:
# temp = cur.next
# cur.next = pre
# pre = cur
# cur = temp
# cur.nex is assign first, left to right
cur.next, pre, cur = pre, cur, cur.next # different from pre, cur, cur.next = cur, cur,next, pre
cnt += 1
continue
# reconnect
elif cnt==n+1:
end = pre
start_pre.next = end
start.next = cur
break
pre = pre.next
cur = cur.next
cnt += 1
return dummy.next
if __name__=="__main__":
length = 3
lst = [ListNode(i+1) for i in range(length)]
for i in xrange(length-1):
lst[i].next = lst[i+1]
print Solution().reverseBetween(lst[0], 1, 3)