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

class Solution {
public:
    /**
     * 
     * @param head ListNode类 
     * @param k int整型 
     * @return ListNode类
     */
    ListNode* reverseKGroup(ListNode* head, int k) {
        // write code here
        ListNode* res = NULL,*tail = NULL;
        int time = 0;
        ListNode* t = head;
        while(t){
            time++;t = t->next;
        }
        time /= k;
        t = head;
        while(time-- > 0){
            ListNode * tem = NULL,*temtai = NULL;
            for(int i=0;i<k;i++){
                if(!tem) {
                    tem = t;temtai = t;
                    t = t->next;
                    temtai->next = NULL;
                }else{
                    ListNode* t1 = t->next;
                    t->next = tem;
                    tem = t;
                    t = t1;
                }
            }
            if(!res){res = tem;tail = temtai;}
            else{
                tail->next = tem;
                tail = temtai;
            }
        }
        
        if(!res) res = t;
        else if(t) tail->next = t;
        
        return res;
    }
};