Split Linked List in Parts

Medium

Description

Split a linked list into k consecutive linked list parts. The parts should have sizes as equal as possible.

Examples

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

Split into 5 parts.

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

When the linked list is empty, it is still need to create k parts, but each part will be empty. This demonstrates the edge case of splitting an empty list.

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

With 5 nodes and k=2, dividing 5÷2=2 remainder 1. Each part gets at least 2 nodes, and the first part gets the extra node (remainder), resulting in parts of size 3 and 2.

Constraints

  • 0 ≤ list length ≤ 1000

Ready to solve this problem?

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