class Solution {
public:
ListNode* reverseKGroup(ListNode* head, int k) {
ListNode* ans = new ListNode(-1);
ans->next = NULL;
auto end = ans;
auto p = head;
int cnt = 0;
while (p != NULL) {
auto t = p;
p = p->next;
t->next = end->next;
end->next = t;
cnt++;
if (cnt == k) {
while (cnt) {
end = end->next;
cnt--;
}
}
}
if (cnt) {
p = end->next;
end->next = NULL;
while (p != NULL) {
auto t = p;
p = p->next;
t->next = end->next;
end->next = t;
}
}
return ans->next;
}
};
转载请注明原文地址:https://blackberry.8miu.com/read-31537.html