Easy to Follow Python Solution


  • 1
    M
    def splitListToParts(self, root, k):
        n = 0
        curr = root
        while curr != None:
            n += 1
            curr = curr.next
        
        # determine part length and any leftover length
        length = n / k
        leftover_length = n % k
        curr = root
        parts = []
        for i in xrange(k):
            part = []
            for j in xrange(length):
                part.append(curr.val)
                curr = curr.next
            if leftover_length > 0:
                part.append(curr.val)
                curr = curr.next
                leftover_length -= 1
            parts.append(part)
        return parts

Log in to reply
 

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