Medium
Split Linked List in Parts — Python
Full explanation · Time O(n + k) · Space O(1)
# Time: O(n + k)
# Space: O(1)
class Solution(object):
def splitListToParts(self, root, k):
"""
:type root: ListNode
:type k: int
:rtype: List[ListNode]
"""
n = 0
curr = root
while curr:
curr = curr.next
n += 1
width, remainder = divmod(n, k)
result = []
curr = root
for i in xrange(k):
head = curr
for j in xrange(width-1+int(i < remainder)):
if curr:
curr = curr.next
if curr:
curr.next, curr = None, curr.next
result.append(head)
return result