class Solution {
public:
ListNode* reverseKGroup(ListNode* head, int k) {
auto dummy = new ListNode(-1, head);
for (auto p = dummy;;) { //注意这里必须要有两个 ; ;
auto q = p;
for (int i = 0; i < k && q; i ++) q = q->next; //是否足k个节点
if (q == NULL) break; //说明不足k个节点
auto a = p->next, b = a->next;
for (int i = 0; i < k - 1; i ++) {
auto c = b->next;
b->next = a; //1 和 2
// c->next = b; 不要加这句,否则错误
a = b, b = c; //为2做准备
}
auto c = p->next;
p->next = a, c->next = b; // 分别是3 和 4
p = c; //5
}
return dummy->next;
}
};