92. Reverse Linked List II
https://leetcode.com/problems/reverse-linked-list-ii/
Python
class Solution:
def reverseBetween(self, head: Optional[ListNode], left: int, right: int) -> Optional[ListNode]:
stack = []
dummy = ListNode(next=head)
cur = dummy
pre, tail = None, None
for i in range(right+1):
if i == left-1:
pre = cur
if i == right:
tail = cur.next
if i >= left and i <= right:
stack.append(cur)
cur = cur.next
cur = None
while stack:
cur = stack.pop()
pre.next = cur
pre = cur
cur.next = tail
return dummy.next