-
Notifications
You must be signed in to change notification settings - Fork 4
/
726.linningmii.py
36 lines (28 loc) · 967 Bytes
/
726.linningmii.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
30
31
32
33
34
35
36
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def splitListToParts(self, root, k):
"""
:type root: ListNode
:type k: int
:rtype: List[ListNode]
"""
val_list = []
node = root
while node is not None:
val_list.append(node.val)
node = node.next
larger_parts_count = len(val_list) % k
normal_part_size = int(len(val_list) / k)
larger_part_size = normal_part_size + 1
result = []
for i in range(k):
if i < larger_parts_count:
result.append(val_list[larger_part_size * i: larger_part_size * (i + 1)])
else:
result.append(val_list[normal_part_size * i + larger_parts_count: normal_part_size * (
i + 1) + larger_parts_count])
return result