[python] Reverse Nodes in K Group
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
- def reverseKGroup(head, k):
- dummy = jump = ListNode(0)
- dummy.next = left = right = head
- while True:
- count = 0
- while right and count < k: # use right to locate the range
- right = right.next
- count += 1
- if count == k: # if size k satisfied, reverse the inneright linked list
- pre, cur = right, left
- for _ in range(k):
- cur.next, cur, pre = pre, cur.next, cur # standard reversing
- jump.next, jump, left= pre, left, right # connect two k-groups
- else:
- return dummy.next
Editor
You can edit this paste and save as new: