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:
Return an array of the k parts.
Example:
Input: head = [1, 2, 3], k = 5
Output: [[1], [2], [3], [], []]
Constraints:
None
representing empty parts.length//k
nodes, except the first length%k
parts which will have one extra node each to handle the remainder.class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def splitListToParts(head: ListNode, k: int):
# Step 1: Calculate the total length of the linked list
length = 0
current = head
while current:
length += 1
current = current.next
# Step 2: Calculate the size of each part
part_length = length // k
remainder = length % k
# Step 3: Split the list
parts = []
current = head
for i in range(k):
part_head = current
part_size = part_length + (1 if i < remainder else 0)
# Move the current pointer to the end of the current part
for j in range(part_size - 1):
if current:
current = current.next
# Break the list if needed
if current:
temp = current.next
current.next = None
current = temp
# Add the current part to the result
parts.append(part_head)
return parts
This solution ensures we split the linked list into k parts as evenly as possible in linear time, adhering to the constraints and requirements set by the problem statement.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?