Skip to content
← All posts

Split Linked List in Parts: Even Distribution with Remainders

3 min read
leetcodeproblemmediumlinked-lists

Split Linked List in Parts is LeetCode problem 725. Given the head of a linked list and an integer k, split the list into k consecutive parts. The parts should be as equal as possible: no two parts should differ in size by more than one, and earlier parts should be at least as large as later parts.

The problem

Given head of a linked list with n nodes, split it into k parts. If n is not divisible by k, the first n % k parts get one extra node each. If k is greater than n, some parts will be null.

Example: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] with k = 3 produces [[1, 2, 3, 4], [5, 6, 7], [8, 9, 10]].

original list (n = 10, k = 3)12345678910cutcutresult1234part 1 (4 nodes)567part 2 (3 nodes)8910part 3 (3 nodes)
A 10-node list split into 3 parts. The first part gets one extra node because 10 % 3 = 1.

The approach: compute sizes then split

  1. Count the total length n
  2. Each part gets base_size = n // k nodes
  3. The first remainder = n % k parts get one extra node
  4. Walk through the list, cutting at the right positions

Visual walkthrough

Step 1: Count the length and compute sizes. n = 10, k = 3. base_size = 10 // 3 = 3, remainder = 10 % 3 = 1.

12345678910base = 3, remainder = 1

Every part gets at least 3 nodes. The first 1 part gets an extra node (3 + 1 = 4).

Step 2: Build part 1. i = 0, which is less than remainder (1), so part_size = 3 + 1 = 4. Walk 3 steps from node 1 to reach node 4, then cut.

12345678910cut here

Part 1 is [1, 2, 3, 4]. Sever the link after node 4. Advance curr to node 5.

Step 3: Build part 2. i = 1, which is not less than remainder (1), so part_size = 3. Walk 2 steps from node 5 to reach node 7, then cut.

12345678910cut here

Part 2 is [5, 6, 7]. Sever the link after node 7. Advance curr to node 8.

Step 4: Build part 3. i = 2, part_size = 3. The remaining nodes [8, 9, 10] form the last part. No more cutting needed.

12345678910part 1 (4)part 2 (3)part 3 (3)

Part 3 is [8, 9, 10]. All 10 nodes are distributed. Result: [[1,2,3,4], [5,6,7], [8,9,10]].

The code

def split_list_to_parts(head, k):
    length = 0
    curr = head
    while curr:
        length += 1
        curr = curr.next

    base_size = length // k
    remainder = length % k

    result = []
    curr = head

    for i in range(k):
        result.append(curr)
        part_size = base_size + (1 if i < remainder else 0)

        for j in range(part_size - 1):
            if curr:
                curr = curr.next

        if curr:
            next_part = curr.next
            curr.next = None
            curr = next_part

    return result

Key details to notice:

  • base_size is the minimum number of nodes per part
  • The first remainder parts each get one extra node
  • You walk part_size - 1 steps to reach the last node of the current part, then sever the link
  • If k is greater than n, the loop naturally appends None for empty parts

The inner loop runs part_size - 1 times because you start at the first node of the part and need to reach the last node. If part_size is 0, the inner loop does not execute and curr remains None.

Complexity analysis

MetricValueReasoning
TimeO(n + k)One pass to count length, one pass to split. The k comes from iterating when some parts are empty.
SpaceO(k)The result array has k entries. No extra data structures beyond that.

The building blocks

Counting linked list length

A simple traversal to count nodes. This same pattern appears whenever you need the total size before processing.

Severing a linked list

Setting curr.next = None to split a list into segments. This technique also appears in:

  • Sort List (LeetCode 148) splitting the list at the midpoint for merge sort
  • Rotate List (LeetCode 61) cutting and reconnecting at a rotation point
  • Reverse Linked List II (LeetCode 92) isolating a sublist for reversal

Edge cases to watch for

  • k greater than n. Extra parts are null. For n = 3, k = 5, result is [[1], [2], [3], null, null].
  • k equals 1. Return the whole list as a single part.
  • k equals n. Each part has exactly one node.
  • Empty list. head is None. Return k null entries.
  • n divisible by k. Each part has exactly n // k nodes, no remainder distribution needed.

From understanding to recall

Split Linked List in Parts is a clean exercise in integer division and remainder distribution. The core idea, giving the first n % k parts an extra node, is a general technique for distributing items as evenly as possible. You will see it in array partitioning and load balancing problems too.

Reading the solution once makes sense, but reproducing the exact loop structure under pressure requires remembering: walk part_size - 1 steps (not part_size), sever the link, and advance to the next part. Spaced repetition drills lock in these boundary details.

Related posts

  • Sort List - Splitting a linked list at the midpoint for merge sort, LeetCode 148
  • Rotate List - Cutting and reconnecting a linked list at a rotation point, LeetCode 61
  • Reverse Linked List II - Isolating and reversing a sublist, LeetCode 92

Visual Learner? Explore how linked list operations work with our Linked List Interactive Visualization.