SHARE
TWEET

Untitled

a guest Jul 22nd, 2019 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Definition for singly-linked list.
  2. # class ListNode:
  3. #     def __init__(self, x):
  4. #         self.val = x
  5. #         self.next = None
  6. class Solution:
  7.     def reverseBetween(self, head: ListNode, m: int, n: int) -> ListNode:
  8.         if m == n: return head
  9.         st = []
  10.         left, node = None, head
  11.         if m != 1:
  12.             for i in range(1, m - 1):
  13.                 node = node.next
  14.             left = node
  15.             node = node.next # position m
  16.        
  17.         for i in range(m, n + 1):
  18.             st.append(node)
  19.             node = node.next
  20.        
  21.         l1, l2 = st.pop(), None
  22.         if m == 1:
  23.             head = l1
  24.         else:
  25.             left.next = l1
  26.        
  27.         while st:
  28.             l2 = st.pop()
  29.             l1.next = l2
  30.             l1 = l2
  31.        
  32.         l1.next = node
  33.        
  34.         return head
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top