Split Linked List in Parts (LeetCode 725): Solution and Explanation
Split a singly linked list into k consecutive parts whose sizes differ by at most one, with longer parts first. Some parts may be empty. Return the k part heads in order.
Intuition
The sizes are decided by arithmetic alone. If the list has n nodes, spreading them over k parts as evenly as possible gives every part n // k nodes, and the leftover n % k nodes go one each to the first n % k parts. That is the only split where sizes differ by at most one and earlier parts are never shorter.
So the problem splits in two. First find n with one walk of the list. Then walk again and cut. For each part, the current node becomes its head, you step forward until the part's last node, and you set that node's next to null so the part ends there.
Before cutting, remember the node after the tail, because that is where the next part starts. When n is smaller than k, the walk runs out early and every remaining part is simply null.
No nodes are copied. The output is k pointers into the original chain, which has been cut into pieces.
Approach: Count, then cut in place
- Walk the list once to count its length
n. - Compute
size = n // kandextra = n % k. - Set
curr = head. For each partifrom 0 to k - 1, recordcurrasparts[i]. - The part's length is
size + 1ifi < extra, elsesize. Movecurrforwardlength - 1times to the part's tail. - If
curris not null, savecurr.next, setcurr.next = None, and continue from the saved node. - Return
parts.
Step-by-step example
head = [1,2,3,4,5,6,7,8,9,10], k = 3
- Counting gives n = 10.
- size = 10 // 3 = 3 and extra = 10 % 3 = 1, so the lengths are 4, 3, 3.
- Part 0 starts at 1. Walk 3 steps to 4 and cut after it: [1,2,3,4].
- Part 1 starts at 5. Walk 2 steps to 7 and cut: [5,6,7].
- Part 2 starts at 8. Walk 2 steps to 10, which already ends the list: [8,9,10].
Code: Python, Java, C++
1class Solution:
2 def splitListToParts(self, head: Optional[ListNode], k: int) -> List[Optional[ListNode]]:
3 n = 0
4 node = head
5 while node:
6 n += 1
7 node = node.next
8 size, extra = divmod(n, k)
9 parts = []
10 curr = head
11 for i in range(k):
12 parts.append(curr)
13 length = size + (1 if i < extra else 0)
14 for _ in range(length - 1):
15 curr = curr.next
16 if curr:
17 nxt = curr.next
18 curr.next = None
19 curr = nxt
20 return parts1class Solution {
2 public ListNode[] splitListToParts(ListNode head, int k) {
3 int n = 0;
4 for (ListNode node = head; node != null; node = node.next) {
5 n++;
6 }
7 int size = n / k, extra = n % k;
8 ListNode[] parts = new ListNode[k];
9 ListNode curr = head;
10 for (int i = 0; i < k; i++) {
11 parts[i] = curr;
12 int length = size + (i < extra ? 1 : 0);
13 for (int j = 0; j < length - 1; j++) {
14 curr = curr.next;
15 }
16 if (curr != null) {
17 ListNode nxt = curr.next;
18 curr.next = null;
19 curr = nxt;
20 }
21 }
22 return parts;
23 }
24}1class Solution {
2public:
3 vector<ListNode*> splitListToParts(ListNode* head, int k) {
4 int n = 0;
5 for (ListNode* node = head; node; node = node->next) {
6 n++;
7 }
8 int size = n / k, extra = n % k;
9 vector<ListNode*> parts(k, nullptr);
10 ListNode* curr = head;
11 for (int i = 0; i < k; i++) {
12 parts[i] = curr;
13 int length = size + (i < extra ? 1 : 0);
14 for (int j = 0; j < length - 1; j++) {
15 curr = curr->next;
16 }
17 if (curr) {
18 ListNode* nxt = curr->next;
19 curr->next = nullptr;
20 curr = nxt;
21 }
22 }
23 return parts;
24 }
25};Time and space complexity
O(n + k)
The list is walked twice, once to count and once to cut, and the loop over parts runs k times even when the list is short.
O(k)
Only the output array of k heads is allocated; the nodes are reused in place.
Other approaches
Copy into new lists (time O(n + k), space O(n))
Build each part from new nodes instead of cutting the original. It leaves the input untouched, which can matter if the caller still needs it, at the cost of O(n) extra memory.
Store nodes in an array first (time O(n + k), space O(n))
Push every node into an array, then compute each part's start index directly and cut at index boundaries. It is simple to reason about but uses O(n) extra space for the array.
Common mistakes
- Giving the extra nodes to the last parts instead of the first ones. Earlier parts must be at least as long as later ones.
- Walking
lengthsteps instead oflength - 1, which lands on the next part's head and cuts one node too late. - Forgetting to cut, so every part still runs to the end of the original list.
- Dereferencing a null
currwhen n < k. Checkcurrbefore cutting and let the remaining parts be null.
Frequently asked questions
Why do the first n % k parts get the extra node?
The problem requires earlier parts to be at least as long as later ones. Handing the leftover nodes out from the front is the only way to satisfy that with sizes that differ by at most one.
What does an empty part look like in the output?
It is a null head, shown as [] in the serialized output. It happens whenever k is larger than the number of nodes.
Can this be done in one pass?
Not without knowing the length in advance or buffering the nodes. The size of the first part depends on n, so you must count first or store the nodes.
What is the time complexity of Split Linked List in Parts?
O(n + k): two walks over the n nodes plus one loop over the k parts, with O(k) space for the result.