Split Linked List in Parts - Problem

Imagine you have a long chain of connected elements, and you need to divide it fairly into k separate groups. This is exactly what we're doing with a linked list!

Given the head of a singly linked list and an integer k, your task is to split the linked list into k consecutive parts. The challenge is to make the split as fair as possible:

  • Equal distribution: No two parts should differ in size by more than one node
  • Front-heavy: If sizes can't be perfectly equal, earlier parts should be larger
  • Order preserved: Maintain the original order of nodes
  • Null parts allowed: Some parts can be empty if k is larger than the list length

Return an array of k ListNode heads, where each head points to the start of one part.

Example: A list of 10 nodes split into 3 parts would create parts of sizes [4, 3, 3] - the first part gets the extra node.

Input & Output

Basic Split Example
$ Input: head = [1,2,3], k = 5
β€Ί Output: [[1],[2],[3],[],[]]
πŸ’‘ Note: The list has 3 nodes but we need 5 parts. Each node becomes its own part, and the remaining 2 parts are null/empty.
Uneven Distribution
$ Input: head = [1,2,3,4,5,6,7,8,9,10], k = 3
β€Ί Output: [[1,2,3,4],[5,6,7],[8,9,10]]
πŸ’‘ Note: 10 nodes split into 3 parts: 10Γ·3 = 3 remainder 1. So sizes are [4,3,3]. The first part gets the extra node.
Empty List Edge Case
$ Input: head = [], k = 3
β€Ί Output: [[],[],[]]
πŸ’‘ Note: When the input list is empty, all k parts will be null/empty arrays.

Visualization

Tap to expand
πŸ• Pizza Distribution SystemConveyor Belt (Linked List)πŸ•πŸ•πŸ•πŸ•πŸ•5 Pizzas TotalDistribution Math5 pizzas Γ· 3 trucks = 1 each5 % 3 = 2 extra pizzasResult: [2, 2, 1] pizzasDelivery Trucks (Output Parts)Truck 1πŸ•πŸ•2 pizzasTruck 2πŸ•πŸ•2 pizzasTruck 3πŸ•1 pizzaCutting Process:First 2 pizzasCUTNext 2 pizzasCUTLast pizzaπŸ’‘ Key Insight: Calculate exact cuts upfront, then traverse once - no repeated work!
Understanding the Visualization
1
Count the Pizzas
First, count all pizzas on the conveyor belt to know what you're working with
2
Calculate Distribution
Determine how many pizzas each truck gets: base amount plus extras for first trucks
3
Load the Trucks
Move along the belt once, cutting it at precise points to fill each truck
4
Handle Extras
If there are more trucks than pizzas, some trucks stay empty
Key Takeaway
🎯 Key Insight: By calculating the mathematical distribution first (length÷k and length%k), we can make precise cuts in a single pass, avoiding the inefficiency of multiple traversals.

Time & Space Complexity

Time Complexity
⏱️
O(n*k)

We traverse n nodes to count, then for each of k parts, we may traverse up to n nodes again to reach the starting position

n
2n
βœ“ Linear Growth
Space Complexity
O(1)

Only using constant extra space for variables, not counting the output array

n
2n
βœ“ Linear Space

Constraints

  • The number of nodes in the list is in the range [0, 1000]
  • 1 ≀ k ≀ 50
  • 0 ≀ Node.val ≀ 1000
  • Follow-up: Can you solve this without modifying the original list structure?
Asked in
Amazon 45 Microsoft 32 Google 28 Meta 22
73.9K Views
Medium Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen