> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Split Linked List in Parts Python Solution

> Tested Python solution for LeetCode 725 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 725, [Medium](/catalog/medium). Topics: [Linked List](/catalog/topics/linked-list). [View on LeetCode](https://leetcode.com/problems/split-linked-list-in-parts/description/).

Generate this problem as a practice environment: tested reference solution, 15 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 725   # by problem number
lcpy gen -s split_linked_list_in_parts   # by problem name
```

## Problem

Given the head of a singly linked list and an integer `k`, split the linked list into `k` consecutive linked list parts.

The length of each part should be as equal as possible: no two parts should have a size differing by more than one. This may lead to some parts being null.

The parts should be in the order of occurrence in the input list, and parts occurring earlier should always have a size greater than or equal to parts occurring later.

Return an array of the `k` parts.

### Examples

```
Input: head = [1,2,3], k = 5
Output: [[1],[2],[3],[],[]]
Explanation: The first element output[0] has output[0].val = 1, output[0].next = null.
The last element output[4] is null, but its string representation as a ListNode is [].
```

```
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]]
Explanation: The input has been split into consecutive parts with size difference at most 1, and earlier parts are a larger size than the later parts.
```

### Constraints

* The number of nodes in the list is in the range \[0, 1000].
* 0 \<= Node.val \<= 1000
* 1 \<= k \<= 50

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/split_linked_list_in_parts/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/split_linked_list_in_parts/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from leetcode_py import ListNode


class Solution:
    # Time: O(n + k)
    # Space: O(k)
    def split_list_to_parts(self, head: ListNode[int] | None, k: int) -> list[ListNode[int] | None]:
        length = 0
        node = head
        while node is not None:
            length += 1
            node = node.next

        width, remainder = divmod(length, k)
        parts: list[ListNode[int] | None] = []
        node = head
        for i in range(k):
            parts.append(node)
            if node is None:
                continue
            part_size = width + (1 if i < remainder else 0)
            for _ in range(part_size - 1):
                if node.next is not None:
                    node = node.next
            next_head = node.next
            node.next = None
            node = next_head
        return parts
```

## Complexity

| Time | Space |
| - | - |
| O(n + k) | O(k) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.