Split Linked List in Parts
Checking your account…
Sign in to save your code and progress across devices. The lesson and problem statement remain public.
Loading the interactive Interview workspace.If it does not appear, the problem and learning material remain readable, but browser execution is unavailable.Reload Interview workspace
Problem
Implement Solution.splitListToParts(head, k). Return k linked-list heads; part sizes differ by at most one, earlier parts are at least as large, and concatenating parts restores input order.
Starter code
class Solution:
def splitListToParts(self, head, k):
passTest cases
more-parts
{
"args": [
{
"$type": "linked-list",
"values": [
1,
2,
3
]
},
5
]
}Expected: [{"$type":"linked-list","values":[1]},{"$type":"linked-list","values":[2]},{"$type":"linked-list","values":[3]},null,null]
Wizard outline
- Step 1: Initialize Solution.splitListToParts
Replace the empty starter with the first real state owned by Solution.splitListToParts. A small, named state is easier to verify than a complete algorithm. Establish it before adding the branch or loop that changes it.
- Step 2: Assemble the primary transition
Extend the initialized state with the next contiguous part of the popular solution. The transition explains how one input element or operation changes the state; boundaries are easier to reason about after this invariant is visible.
- Step 3: Pass the One Node Parts case
Complete the readable core algorithm for one representative Interview case. Distribute quotient nodes to every part and one extra node to the first remainder parts.
- Step 4: Harden the Uneven boundary
Repair the reviewed boundary and pass the complete submission contract. Every part receives base nodes and exactly the first remainder receive one extra, satisfying size rules and totaling all nodes. Sequential cuts preserve original order and make parts independent.
Footguns and prerequisites
- Set each part tail.next to None before moving on.
- linked lists
Reviewed references
Practice prerequisites
- Relink Nodes Safely(opens in a new tab)
Relink Nodes Safely isolates every untouched node keeps its original next link, and at most one predecessor changes to the removed node’s former successor. That focused state discipline is required when implementing split linked list parts as a complete Interview Problem.
Recommended approach and implementation
Count nodes, compute base and remainder with divmod, then for each part advance its size and cut the tail link.
Why it works: Every part receives base nodes and exactly the first remainder receive one extra, satisfying size rules and totaling all nodes. Sequential cuts preserve original order and make parts independent.
class Solution:
def splitListToParts(self, head, k):
"""
Checkpoint 1: initialize the state owned by this Interview contract.
Checkpoint 2: assemble the primary transition without hiding the boundary.
"""
length = 0
node = head
while node:
length += 1
node = node.next
base, extra = divmod(length, k)
parts = []
current = head
for index in range(k):
parts.append(current)
size = base + (1 if index < extra else 0)
for _ in range(size - 1):
current = current.next
if current:
following = current.next
current.next = None
current = following
return parts