SHARE
TWEET

Untitled

a guest Oct 23rd, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def reverseList(head, tail):
  2.     prev = None
  3.     while prev != tail:
  4.         prev, prev.next, head = head, prev, head.next
  5.     return prev
  6.    
  7. def reverseNodesInKGroups(l, k):
  8.     if k < 2:
  9.         return l
  10.    
  11.     p = ListNode(-1)
  12.     p.next = l
  13.     ret = p
  14.     while True:
  15.         flag = True
  16.         tmp = p
  17.         for i in range(k):
  18.             if tmp.next:
  19.                 tmp = tmp.next
  20.             else:
  21.                 flag = False
  22.                 break
  23.        
  24.         if flag:
  25.             q = tmp.next
  26.             t = p.next
  27.             reverseList(t, tmp)
  28.             p.next = tmp
  29.             t.next = q
  30.             p = t
  31.         else:
  32.             break
  33.    
  34.     return ret.next
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