vector<ListNode *> splitListToParts(ListNode *root, int k) {
vector<ListNode *> vec;
if (root == nullptr) {
for (; k > 0; --k)
vec.push_back({});
return vec;
}
int len = 0;
ListNode *it = root;
for (; it != nullptr; it = it->next, ++len);
int nums = len / k;
len -= (nums * k);
if (nums == 0) {
for (; k > 0; --k)
if (root) {
vec.push_back(root);
ListNode *temp = root->next;
root->next = nullptr;
root = temp;
} else
vec.push_back({});
return vec;
}
for (; k > 0; --k) {
ListNode *temp_it = root;
for (int temp = nums - 1; temp > 0; --temp)
temp_it = temp_it->next;
if (len > 0) {
temp_it = temp_it->next;
--len;
}
ListNode *temp_root = temp_it->next;
temp_it->next = nullptr;
vec.push_back(root);
root = temp_root;
}
return vec;
}