- See Code
O(n)
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def splitListToParts(self, root: ListNode, k: int) -> List[ListNode]:
# Count the length of the linked list
curr, length = root, 0
while curr:
curr, length = curr.next, length + 1
# Determine the length of each chunk
chunk_size, longer_chunks = length // k, length % k
res = [chunk_size + 1] * longer_chunks + [chunk_size] * (k - longer_chunks)
# Split up the list
prev, curr = None, root
for index, num in enumerate(res):
if prev:
prev.next = None
res[index] = curr
for i in range(num):
prev, curr = curr, curr.next
return res