python


  • 0
    A
    class Solution(object):
        def reverseKGroup(self, head, k):
            """
            :type head: ListNode
            :type k: int
            :rtype: ListNode
            """
            dummy = ListNode(-1)
            dummy.next, tail = head, dummy
            p = q = head
            i = 0
            while p:
                p = p.next
                i += 1
                if i == k:
                    prev = p
                    while q.next != p:
                        q.next, q, prev = prev, q.next, q
                    tail.next, tail, q.next, q, i = q, tail.next, prev, p, 0
            return dummy.next
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.