-
Notifications
You must be signed in to change notification settings - Fork 177
/
725.py
29 lines (28 loc) · 795 Bytes
/
725.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution:
def splitListToParts(self, root: ListNode, k: int) -> List[ListNode]:
n = 0
node = root
while node:
n += 1
node = node.next
count = n // k
residual = n % k
i = 0
ret = [[] for _ in range(k)]
prev = root
while prev and k > 0:
node = prev
leftover = count
ret[i] = node
i += 1
while node and leftover > 1:
node = node.next
leftover -= 1
if node and count != 0 and residual:
node = node.next
residual -= 1
prev = node.next if node else None
if node:
node.next = None
k -= 1
return ret