-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplit-linked-list.py
44 lines (37 loc) · 1.29 KB
/
split-linked-list.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
37
38
39
40
41
42
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def splitListToParts(self, head: Optional[ListNode], k: int) -> List[Optional[ListNode]]:
lists = [None for _ in range(k)]
length = 0
temp_head = head
while temp_head is not None:
length += 1
temp_head = temp_head.next
remainder = (length % k) if length > k else 0
node_size = max(length // k, 1)
temp_head = head
index = 0
ctr = node_size + (1 if remainder != 0 else 0)
remainder = max(remainder - 1, 0)
lists[0] = temp_head
while temp_head is not None:
ctr -= 1
if ctr == 0:
ctr = node_size + (1 if remainder != 0 else 0)
remainder = max(remainder - 1, 0)
index += 1
tmp = temp_head.next
temp_head.next = None
temp_head = tmp
try:
lists[index] = temp_head
except IndexError:
pass
else:
temp_head = temp_head.next
# print(lists)
return lists