class Solution {
public:
ListNode* reverseKGroup(ListNode* head, int k) {
if(head == NULL) return head;
ListNode* a = head;
ListNode* b = head;
for(int i = 0;i < k ;i++){
if(b == NULL) return head;
b = b->next;
}
ListNode* newHead = reverse(a, b);
a->next = reverseKGroup(b,k);
return newHead;
}
ListNode* reverse(ListNode* a, ListNode* b){
ListNode* cur = a;
ListNode* pre = NULL;
ListNode* nxt = a;
while(cur != b){
nxt = cur->next;
cur->next = pre;
pre = cur;
cur = nxt;
}
return pre;
}
};