Skip to content
Decoder

Split Linked List in Parts

Medium

Problem

You get the head of a linked list and a number k. Split the list into exactly `k` consecutive parts and return them as an array of heads. The part sizes must be as equal as possible: no two parts differ in size by more than one, and earlier parts are never smaller than later ones. If there are fewer nodes than k, some parts are empty (None in Python, null in TypeScript).

Example 1

Input
1
Output
2 -> 3, k = 5 → [1], [2], [3],
Why
empty), (empty

Example 2

Input
1
Output
2 -> ... -> 10, k = 3 → [1, 2, 3, 4], [5, 6, 7], [8, 9, 10]

The tests show each part as a list of values, and an empty part as None / null.

Constraints

  • 0 <= number of nodes <= 1000
  • 0 <= Node.val <= 1000
  • 1 <= k <= 50

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.