/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 * };
 */

class Solution {
public:
    /**
     * 
     * @param head ListNode类 
     * @param k int整型 
     * @return ListNode类
     */
    ListNode* reverseKGroup(ListNode* head, int k) {
        ListNode* dummyHead=(ListNode*)malloc(sizeof(ListNode));
        dummyHead->next=NULL;
        ListNode* pre=NULL,*cur=head,*next=NULL,*proxyHead=dummyHead,*t=NULL;
        int count=1;
        while(cur!=NULL){
            next=cur->next;
            if(count==1){
                t=cur;
            }
            cur->next=pre;
            if(count%k==0){
                proxyHead->next=cur;
                proxyHead=t;
                count=1;
                pre=NULL;
                cur=next;
                continue;
            }
            pre=cur;
            cur=next;
            count++;
        }
        if((count-1)%k==0){
            proxyHead->next=pre;
        }else{
            while(pre!=NULL){
                next=pre->next;
                pre->next=proxyHead->next;
                proxyHead->next=pre;
                pre=next;
            }
        }
        return dummyHead->next;
    }
};