Split Linked List in Parts
Split the list into k consecutive parts whose lengths differ by at most 1, with the earlier parts at least as long as the later ones. If there are fewer than k nodes, the last parts are empty. Return a Python list of k lists holding the values of each part, in order. The list is passed in as head.
Linked lists appear in the tests as the list of their values, like [1,2,3]. Each node is a ListNode with .val and .next; the class is already defined for you.
Example 1
head [1,2,3,4,5,6,7]
k 3
returns [[1,2,3],[4,5],[6,7]]
Lengths 3, 2, 2.
Example 2
head [1,2]
k 4
returns [[1],[2],[],[]]
Two parts get one node each, two are empty.
Constraints
0 <= number of nodes <= 1001 <= k <= 50-100 <= Node.val <= 100
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.