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
| class Solution { public: ListNode* reverseKGroup(ListNode* head, int k) { if (head == nullptr || k == 1) { return head; } auto n = head; ListNode *g = nullptr; while (n != nullptr) { ListNode *t = nullptr; ListNode *h = nullptr; int i; for (i = 0; i < k && n != nullptr; i++) { if (i == 0) { t = n; } auto next = n->next; n->next = h; h = n; n = next; } if (n == nullptr && i < k) { n = h; h = nullptr; while (n != nullptr) { auto next = n->next; n->next = h; h = n; n = next; } } if (g != nullptr) { g->next = h; } else { head = h; } g = t; } return head; } };
|