Python EZ understanding Recursive


  • 0
    class Solution(object):
        def reverseKGroup(self, head, k):
            h, kk = head, k
            while h and kk:
                h = h.next
                kk -= 1
            if kk == 0: #if this baby step is "takable", than just reverse k element, and connect the first with answer beyond k
                h, pre = head, None
                for i in xrange(k):
                    h.next, h, pre = pre, h.next, h
                head.next = self.reverseKGroup(h, k)
                return pre
            else:# No need to do anything
                return head
    

Log in to reply
 

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