Super fast and Straightforward Python Solution


  • 0

    Simply "read" the LinkedList Node of appropriate length into each part

    class Solution(object):
        def splitListToParts(self, root, k):
            """
            :type root: ListNode
            :type k: int
            :rtype: List[ListNode]
            """
            num, curr = 0, root
            while curr is not None:
                num += 1
                curr = curr.next
            res = []
            prev, curr = None, root
            for i in range(k):
                sub_len = num / k + 1 if i < num % k else num / k
                res.append(curr)
                for j in range(sub_len):
                    prev = curr
                    curr = curr.next
                if prev is not None:
                    prev.next = None
            return res
    

Log in to reply
 

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