1
class Solution {
2
public:
3
vector<ListNode *> splitListToParts(ListNode *head, int k) {
4
vector<ListNode *> ans;
5
int len = 0;
6
ListNode *temp = head;
7
while (temp != NULL) len++, temp = temp->next;
8

9
int y = len / k, z = len % k;
10

11
while (head != NULL) {
12
ans.push_back(head);
13
int count = 1;
14
while (head != NULL && count < y) head = head->next, count++;
15

16
if (z && y) {
17
head = head->next;
18
z--;
19
}
20
if (head == NULL) continue;
21
ListNode *temp = head->next;
22
head->next = NULL;
23
head = temp;
24
}
25
while (ans.size() < k) {
26
ans.push_back(NULL);
27
}
28

29
return ans;
30
}
31
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0