Split Linked List in Parts

MediumArrayLinked ListGraphMatrix

Description

Split a linked list into k consecutive linked list parts. The parts should have sizes as equal as possible. Return the result as a 2D array.

Examples

Input:head = [1,2,3], k = 5
Output:[[1],[2],[3],[],[]]
Explanation:

List has 3 nodes, k=5 parts. First 3 parts get 1 node each: [1],[2],[3]. Last 2 parts are empty: [],[].

Input:head = [], k = 3
Output:[[],[],[]]
Explanation:

When the linked list is empty, all 3 parts are empty.

Input:head = [1,2,3,4,5], k = 2
Output:[[1,2,3],[4,5]]
Explanation:

With 5 nodes and k=2, the first part gets 3 nodes and the second part gets 2 nodes.

Constraints

  • 0 ≤ list length ≤ 1000

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!