Split Linked List In Parts Problem
Description
LeetCode Problem 725.
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.
Example 1:
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 [].
Example 2:
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
Approach: Count, Then Cut
The sizes of the parts depend only on the length of the list, so count the nodes first. With length nodes and k parts, every part gets base = length / k nodes, and the extra = length % k leftover nodes go one each to the first extra parts. That makes sizes differ by at most one, with the larger parts first.
Then walk the list once more and cut it in place:
- For part
i, record the current node as the part's head. - Its size is
base + 1ifi < extra, elsebase. - Move forward
size - 1nodes to the part's last node, remember itsnext, setnexttonullto cut the list, and continue from the remembered node.
When k is larger than length, base is 0, so the later parts have size 0. The current node is then null, which is exactly the empty part we must return, and the cutting step is skipped.
The mistake to avoid is walking size steps instead of size - 1: we need to stop on the part's last node to cut after it. Also cut every part, or the first part would still be linked to the rest of the list.
Example: 10 nodes and k = 3 give base = 3 and extra = 1, so the sizes are 4, 3, 3: [1,2,3,4], [5,6,7], [8,9,10].
Complexity: O(n + k) time, where n is the number of nodes, and O(k) space for the output array (no new nodes are created).
# 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 = 0
node = head
while node:
length += 1
node = node.next
base, extra = divmod(length, k) # the first `extra` parts get one more node
parts = []
node = head
for i in range(k):
parts.append(node)
size = base + (1 if i < extra else 0)
# Walk to the last node of this part and cut the list after it.
for _ in range(size - 1):
node = node.next
if node:
next_part = node.next
node.next = None
node = next_part
return parts