-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy path92-reverse-linked-list-ii.py
38 lines (35 loc) · 1.17 KB
/
92-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
# 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]:
if not head or not head.next:
return head
dummy = ListNode(next=head)
count = 0
prev, cur = dummy, head
prev_nodes, old_head, old_tail, next_nodes = None, None, None, None
while cur:
count += 1
if left <= count <= right:
if count == left:
prev_nodes = prev
old_head = cur
if count == right:
old_tail = cur
next_nodes = cur.next
nxt = cur.next
cur.next = prev
prev = cur
cur = nxt
else:
prev = cur
cur = cur.next
prev_nodes.next = old_tail
old_head.next = next_nodes
return dummy.next
# time O(n)
# space O(1)
# using linked list and two pointers (prev and cur) and multi pointers