-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0725.cpp
More file actions
34 lines (30 loc) · 860 Bytes
/
Copy path0725.cpp
File metadata and controls
34 lines (30 loc) · 860 Bytes
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
class Solution {
public:
vector<ListNode *> splitListToParts(ListNode *head, int k) {
int size = 0, part, extra;
for (ListNode *tmp = head; tmp; tmp = tmp->next)
size++;
if (k >= size) {
part = 1;
extra = 0;
} else {
part = size / k;
extra = size - (part * k);
}
vector<ListNode *> res;
ListNode *crnt = head, *tmp;
while (size >= part) {
res.push_back(crnt);
for (int i = 1; i < part; i++)
crnt = crnt->next;
if (extra-- > 0) crnt = crnt->next, size--;
size -= part;
tmp = crnt->next;
crnt->next = nullptr;
crnt = tmp;
}
while (res.size() < k)
res.push_back(nullptr);
return res;
}
};