-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverseLinkedListII.py
More file actions
34 lines (26 loc) · 894 Bytes
/
Copy pathreverseLinkedListII.py
File metadata and controls
34 lines (26 loc) · 894 Bytes
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
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def reverseBetween(self, head: ListNode, left: int, right: int) -> ListNode:
if left == right: return head
dummy = ListNode(0)
dummy.next = head
# Move prev to the node just before the reversal start position
prev = dummy
for _ in range(left-1):
prev = prev.next
next_node = None
curr = prev.next
tail = curr
# Reverse the sublist from left to right
for _ in range(right-left+1):
tmp = curr.next
curr.next = next_node
next_node = curr
curr = tmp
# Connect the reversed portion back to the main list
prev.next = next_node
tail.next = curr
return dummy.next