1class Solution {2public:3vector<ListNode *> splitListToParts(ListNode *head, int k) {4vector<ListNode *> ans;5int len = 0;6ListNode *temp = head;7while (temp != NULL) len++, temp = temp->next;89int y = len / k, z = len % k;1011while (head != NULL) {12ans.push_back(head);13int count = 1;14while (head != NULL && count < y) head = head->next, count++;1516if (z && y) {17head = head->next;18z--;19}20if (head == NULL) continue;21ListNode *temp = head->next;22head->next = NULL;23head = temp;24}25while (ans.size() < k) {26ans.push_back(NULL);27}2829return ans;30}31};