/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类
* @param k int整型
* @return ListNode类
*/
#include <stdlib.h>
struct ListNode* reverseKGroup(struct ListNode* head, int k ) {
// write code here
struct ListNode *result=head,*save;
bool flag=false;
while(1)
{
int cnt=k;
struct ListNode *tail=head;
for(;cnt>0;cnt--)
{
if(tail==NULL)break;
tail=tail->next;
}
if(!cnt)
{
struct ListNode *temp,*o=(struct ListNode*)malloc(sizeof(struct ListNode)),*uil;
uil=head;
for(int i=0;i<k;i++)
{
temp=head;
head=head->next;
temp->next=o->next;
o->next=temp;
}
if(!flag)result=temp,flag=true,save=uil;
else save->next=temp,save=uil;
head=tail;
uil->next=head;
}
else break;
}
return result;
}