ListNode * reverseKGroup(ListNode* head, int k){ if (!head) return head; bool flag = true; ListNode* h = head; for (int i = 0; i < k; i++) { if (!h) { flag = false; break; } h = h->next; } if (!flag) return head; h = head; vector<ListNode *> collection; for (int i = 0; i < k; i++) { collection.push_back(h); h = h->next; } for (int i = 0; i < k; i++) { if (i == 0) { collection[i]->next = reverseKGroup(h, k); } else { collection[i]->next = collection[i - 1]; } } return collection.back(); }