Skip to content

Instantly share code, notes, and snippets.

@Desolve
Created July 10, 2019 16:53
Show Gist options
  • Save Desolve/eeb85ba29bb9832ce6143040c9a799be to your computer and use it in GitHub Desktop.
Save Desolve/eeb85ba29bb9832ce6143040c9a799be to your computer and use it in GitHub Desktop.
0092 Reverse Linked List II
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def reverseBetween(self, head: ListNode, m: int, n: int) -> ListNode:
if m == n: return head
st = []
left, node = None, head
if m != 1:
for i in range(1, m - 1):
node = node.next
left = node
node = node.next # position m
for i in range(m, n + 1):
st.append(node)
node = node.next
l1, l2 = st.pop(), None
if m == 1:
head = l1
else:
left.next = l1
while st:
l2 = st.pop()
l1.next = l2
l1 = l2
l1.next = node
return head
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment