/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
class Solution {
public:
/**
*
* @param head ListNode类
* @param k int整型
* @return ListNode类
*/
ListNode* ans(ListNode *head){
ListNode* p=head;
ListNode* q=head->next;
ListNode* r=head->next->next;
p->next=nullptr;
while(r!=nullptr){
q->next=p;
p=q;
q=r;
r=r->next;
}
q->next=p;
return q;
}
ListNode* reverseKGroup(ListNode* head, int k) {
if(k==1){
return head;
}
ListNode* p=head;
ListNode* r=head;
int t=1;
while(p!=nullptr){
if(t==k){
ListNode* m=p->next;
p->next=nullptr;
if(r==head){
head=ans(r);
}
else{
ListNode* n=r->next;
r->next=ans(r->next);
r=n;
}
r->next=m;
p=m;
t=1;
}
else{
p=p->next;
t++;
}
}
return head;
}
};