//   function ListNode(x){
//     this.val = x;
//     this.next = null;
//   }


/**
  * 
  * @param head ListNode类 
  * @param k int整型 
  * @return ListNode类
  */
function reverseKGroup( head ,  k ) {
    // write code here
    if(head == null) return;
    let dummy_a=head,dummy_b=head;
    for(let i=0;i<k;i++){
        if(dummy_b==null) return head;
        dummy_b=dummy_b.next;
    }
    
    const newHead=reverseNode(dummy_a,dummy_b);
    
    dummy_a.next=reverseKGroup(dummy_b,k);
    
    return newHead;
}
    
function reverseNode(a,b){
    let prev=null,cur=a;
    while(cur!==b){
        const next=cur.next;
        cur.next=prev;
        prev=cur;
        cur=next;
    }
    return prev;
}


module.exports = {
    reverseKGroup : reverseKGroup
};