/** *
- @param head ListNode类
- @param k int整型
- @return ListNode类 */
function reverseKGroup( head , k ) { // write code here if(head ==null|| head.next ==null) return head;
const dummy=new ListNode(0);
dummy.next=head;
let prev=dummy,end=dummy;
while (end.next !== null) {
for(let i=0;i<k&&end!==null;i++){
end=end.next;
}
if(end==null) break;
let next=end.next;
end.next=null;
let start=prev.next;
prev.next=reverse(start);
start.next=next;
prev=start;
end=start;
}
return dummy.next;
}
// 翻转链表
function reverse(head){ if(head==null||head.next==null) return head;
let prev=null,cur=head;
while(cur!==null){
const temp=cur.next;
cur.next=prev;
prev=cur;
cur=temp;
}
return prev;
}
module.exports = { reverseKGroup : reverseKGroup };