Problem 386
Split Linked List into Parts
Medium · 20 pts · Linked List
Problem
Split a linked list into exactly K consecutive parts whose lengths are as equal as possible: no two parts may differ in length by more than one, and earlier parts are never shorter than later parts. Some parts may be empty if the list has fewer than K nodes. Print the K parts in order, one per line. For N nodes, each part gets floor(N / K) nodes, and the first N mod K parts get one extra node.
Input
First line: N and K. Second line: the N node values (empty if N = 0).
Output
Print K lines, each holding one part's values separated by spaces, or EMPTY for an empty part.
Constraints
0 <= N <= 1000; 1 <= K <= 50; 0 <= value <= 1000
Example input
10 3 1 2 3 4 5 6 7 8 9 10
Example output
1 2 3 4 5 6 7 8 9 10
Explanation
Splitting 1 2 3 4 5 6 7 8 9 10 into 3 parts gives sizes 4, 3 and 3: 1 2 3 4, then 5 6 7, then 8 9 10.