-
Notifications
You must be signed in to change notification settings - Fork 481
/
Copy path0092.py
65 lines (55 loc) · 1.31 KB
/
0092.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
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def reverseBetween(self, head, m, n):
"""
:type head: ListNode
:type m: int
:type n: int
:rtype: ListNode
"""
if head == None or head.next == None or m >= n or m < 0 or n < 0:
return head
h = ListNode(-1)
h.next = head
pre = h
cur = head
i = 1
while i < m and cur != None:
pre = cur
cur = cur.next
i += 1
t1 = pre
t2 = cur
while i <= n and cur != None:
lat = cur.next
cur.next = pre
pre = cur
cur = lat
i += 1
t1.next = pre
t2.next = cur
return h.next
def createList():
head = ListNode(0)
cur = head
for i in range(1, 10):
cur.next = ListNode(i)
cur = cur.next
return head
def printList(head):
cur = head
while cur != None:
print(cur.val, '-->', end='')
cur = cur.next
print('NULL')
if __name__ == "__main__":
head = createList()
printList(head)
m = 2
n = 6
res = Solution().reverseBetween(head, m, n)
printList(res)