-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathReverse Linked List II.py
67 lines (60 loc) · 1.77 KB
/
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
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reverseBetween(self, head: Optional[ListNode], left: int, right: int) -> Optional[ListNode]:
# revrese api
def reverse(start, end):
prev = None
cur = start
while prev != end:
nextNode = cur.next
cur.next = prev
prev = cur
cur = nextNode
return prev
if not head or not head.next or right <= left:
return head
start, end = head, head
node, afterRight = 0, 0
# At the begining
if left == 1:
# start index
inc = left-1
while inc > 0:
start = start.next
inc -= 1
# end index
inc = right-1
while inc > 0:
end = end.next
inc -= 1
afterRight = end.next
reverse(start, end)
head = end
else: # Left other then begining
# start index
inc = left-2
while inc > 0:
start = start.next
inc -= 1
# end index
inc = right-1
while inc > 0:
end = end.next
inc -= 1
afterRight = end.next
begin = start # node before left
start = start.next
reverse(start, end)
begin.next = end
# If their's ll chain left agter right chain it to the updated ll
if afterRight:
start.next = afterRight
return head
"""
TC : O(n)
Sc : O(1)
"""