-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLeetcode92.py
28 lines (27 loc) · 903 Bytes
/
Leetcode92.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
class Leetcode92:
succ = None
def reverseBetween(self, head: Optional[ListNode], left: int, right: int) -> Optional[ListNode]:
# Iterate
# dummy = ListNode(0, head)
# guard = dummy
# for i in range(left-1):
# guard = guard.next
# cur = guard.next
# for i in range(left, right):
# nextNode = cur.next
# cur.next = nextNode.next
# nextNode.next = guard.next
# guard.next = nextNode
# return dummy.next
# Recursion
global succ
if left != 1:
head.next = self.reverseBetween(head.next, left-1, right-1)
return head
if right == 1:
succ = head.next
return head
last = self.reverseBetween(head.next, left, right-1)
head.next.next = head
head.next = succ
return last