membalikkan sublist linklist
def reverseBetween(self, head: Optional[ListNode], left: int, right: int) -> Optional[ListNode]:
def reverse(head):
prev = None
cur = head
while cur:
nxt = cur.next
cur.next = prev
prev = cur
cur = nxt
return prev
if left == right:
return head
start = None
startp = None
end = None
endn = None
count = 1
cur = head
while cur and count <= right:
if count < left:
startp = cur
if count == left:
start = cur
if count == right:
end = cur
endn = cur.next
cur = cur.next
count += 1
end.next = None
end = reverse(start)
if startp:
startp.next = end
else:
head = end
start.next = endn
return head
Defiant Donkey