1 class Solution:
 2     def splitListToParts(self, root: 'ListNode', k: int) -> 'List[ListNode]':
 3         N = 0
 4         cur = root
 5         while cur != None:
 6             N += 1
 7             cur = cur.next
 8         mod = N % k
 9         size = N // k
10         ret = []
11         cur = root
12         i = 0
13         while i < k:
14             if cur != None:
15                  ret.append(cur)
16             else:
17                 ret.append([])
18             apdx = 1 if mod > 0 else 0
19             mod -= 1
20             curSize = size + apdx
21             for j in range(curSize-1):
22                 cur = cur.next
23             if cur != None:
24                 nexNode = cur.next
25                 cur.next = None
26                 cur = nexNode
27             i += 1
28         return ret

 

posted on 2019-06-22 09:45  Sempron2800+  阅读(156)  评论(0编辑  收藏  举报