725. Split Linked List in Parts - Explanation

Problem Link



Prerequisites

Before attempting this problem, you should be comfortable with:

  • Linked List Traversal - Need to traverse the list to count nodes and split at specific positions
  • Linked List Manipulation - Severing links by setting next pointers to null to create separate parts
  • Division and Modulo Operations - Used to calculate base part size (n / k) and distribute remainder (n % k)

1. Convert To Array

Intuition

To split the list into k parts as evenly as possible, we first need to know the total length. Converting the linked list to an array gives us random access, making it easy to determine where each part starts and ends. Each part gets n / k elements at minimum, and the first n % k parts get one extra element to distribute the remainder evenly.

Algorithm

  1. Traverse the linked list and store all nodes in an array.
  2. Calculate base_len = n / k (minimum elements per part) and remainder = n % k (parts that get an extra element).
  3. For each of the k parts:
    • If there are remaining elements, set the part's head to arr[start].
    • Compute the tail index: start + base_len - 1, plus 1 if this part gets an extra element.
    • Sever the link by setting arr[tail].next = null.
    • Update start for the next part.
  4. Return the array of part heads.
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def splitListToParts(self, head: Optional[ListNode], k: int) -> List[Optional[ListNode]]:
        arr, cur = [], head
        while cur:
            arr.append(cur)
            cur = cur.next

        N = len(arr)
        base_len, remainder = N // k, N % k

        res = [None] * k
        start = 0
        for i in range(k):
            if start < N:
                res[i] = arr[start]
                tail = start + base_len - 1
                if remainder:
                    tail += 1
                    remainder -= 1
                arr[tail].next = None
                start = tail + 1

        return res

Time & Space Complexity

  • Time complexity: O(n)O(n)
  • Space complexity: O(n)O(n)

2. Iteration

Intuition

We can avoid the extra array by working directly with the linked list. First, count the total nodes. Then traverse again, splitting the list into parts by tracking the current node and severing links at the appropriate positions. The first remainder parts each have one more node than the remaining parts.

Algorithm

  1. Count the total number of nodes in the list.
  2. Calculate base_len = length / k and remainder = length % k.
  3. Traverse the list to create k parts:
    • Record the current node as the head of this part.
    • Advance base_len - 1 + (1 if remainder > 0 else 0) steps to reach the tail.
    • Save curr.next, set curr.next = null to sever, then continue from the saved node.
    • Decrement remainder after each extra-length part.
  4. Return the array of part heads.
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def splitListToParts(self, head: Optional[ListNode], k: int) -> List[Optional[ListNode]]:
        length, curr = 0, head
        while curr:
            curr = curr.next
            length += 1

        base_len, remainder = length // k, length % k
        curr, res = head, []

        for i in range(k):
            res.append(curr)
            for j in range(base_len - 1 + (1 if remainder else 0)):
                if not curr:
                    break
                curr = curr.next
            remainder -= 1 if remainder else 0
            if curr:
                curr.next, curr = None, curr.next

        return res

Time & Space Complexity

  • Time complexity: O(n)O(n)
  • Space complexity:
    • O(1)O(1) extra space.
    • O(n)O(n) space for the output array.

Common Pitfalls

After determining where each part ends, you must set the tail node's next pointer to null. Failing to do this leaves the parts still connected, causing the output to contain overlapping or incorrectly sized lists instead of independent segments.

Incorrect Distribution of Extra Nodes

When n % k != 0, the first remainder parts should each have one extra node. A common mistake is distributing extra nodes to the wrong parts (e.g., the last parts instead of the first) or failing to decrement the remainder counter properly, resulting in unevenly sized parts.

Not Handling Empty Parts When k > n

When there are more parts than nodes, some parts must be empty (null). Forgetting to account for this case can lead to index out of bounds errors or incorrect assignments when trying to set heads for parts that have no nodes.