-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path725.cpp
More file actions
33 lines (33 loc) · 884 Bytes
/
Copy path725.cpp
File metadata and controls
33 lines (33 loc) · 884 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
class Solution {
public:
vector<ListNode *> splitListToParts(ListNode *root, int k) {
vector<ListNode *> ret;
unsigned int size = 0;
ListNode *head = root;
while (head) {
++size;
head = head->next;
}
unsigned int part = k >= size ? 1 : size / k;
unsigned int left = k >= size ? 0 : size % k;
head = root;
while (head) {
int i = 1;
ListNode *tail = head;
while (i < part) {
tail = tail->next;
++i;
}
if (left > 0) {
tail = tail->next;
--left;
}
ret.push_back(head);
head = tail->next;
tail->next = nullptr;
}
while (ret.size() < k)
ret.push_back(nullptr);
return ret;
}
};