725. Split Linked List in Parts
Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 70% Topics: Linked List
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# 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
Solution from kamyu104/LeetCode-Solutions · MIT
Similar questions