# Simple python solution

• ``````    def splitListToParts(self, root, k):
res = []
self.splitListToParts_rec(root, k, res)
return res

def splitListToParts_rec(self, root, k, res):
if root is None:
if k > 0:
res.append([])
self.splitListToParts_rec(root, k - 1, res)
return

n, node = self.size(root), root
dm = divmod(n, k)
total = dm[0]
if dm[1] != 0:
total += 1
for i in range(total - 1):
node = node.next
rec_node = node.next
node.next = None
res.append(root)
self.splitListToParts_rec(rec_node, k - 1, res)

def size(self, node):
if node is None: return 0
return 1 + self.size(node.next)
``````

• My Python3 attempt without recursion

``````class Solution:
def splitListToParts(self, root, k):
if root is None: return [[] for i in range(k)]
length = 1
length_ptr = root
while length_ptr.next is not None:
length_ptr = length_ptr.next
length += 1
group_size = int(length // k)
overflow_group_count = length % k
out = []
ptr = root
start = root
for i in range(k):
if start is None:
out.append(None)
elif overflow_group_count > 0:
overflow_group_count -= 1
for j in range(group_size):
ptr = ptr.next
out.append(start)
start = ptr.next
ptr.next = None
ptr = start
else:
for j in range(group_size-1):
ptr = ptr.next
out.append(start)
start = ptr.next
ptr.next = None
ptr = start
return out
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.