-
Notifications
You must be signed in to change notification settings - Fork 3
/
725.cpp
46 lines (43 loc) · 1.24 KB
/
725.cpp
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
43
44
45
46
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
vector<ListNode*> splitListToParts(ListNode* head, int k) {
int cnt = 0;
ListNode* current = head;
while (current) {
cnt++;
current = current->next;
}
int size = cnt / k;
int remain = cnt % k;
vector<ListNode*> res;
for (int i = 0; i < k; ++i) res.push_back(new ListNode());
vector<ListNode*> temp = res;
current = head;
int index = 0;
int count = size + ((remain > 0) ? 1 : 0);
remain--;
while (current) {
temp[index]->next = new ListNode(current->val);
temp[index] = temp[index]->next;
count--;
if (count == 0) {
index++;
count = size + ((remain > 0) ? 1 : 0);
remain--;
}
current = current->next;
}
for (auto& node : res) node = node->next;
return res;
}
};