FunctionRacerProblemsRace

Split Linked List in Parts

MediumLinked listsPython, JavaScript, TypeScript, Java, C++

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

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Linked lists problems