/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
class Solution {
public:
ListNode* reverseKGroup(ListNode* head, int k) {
ListNode *tail = head;
for (int i=0; i<k; i++) {
if (tail== nullptr) {
return head;
}
tail = tail->next;
}
ListNode *phead = nullptr;
ListNode* cur = head;
while (cur!=tail) {
ListNode *temp = cur->next;
cur->next = phead;
phead = cur;
cur = temp;
}
head->next = reverseKGroup(tail, k);
return phead;
}
};

京公网安备 11010502036488号