题解 | #链表中的节点每k个一组翻转#
链表中的节点每k个一组翻转
http://www.nowcoder.com/practice/b49c3dc907814e9bbfa8437c251b028e
/*
* function ListNode(x){
* this.val = x;
* this.next = null;
* }
*/
/**
*
* @param head ListNode类
* @param k int整型
* @return ListNode类
*/
function reverseKGroup( head , k ) {
// write code here
let pre = null
let current = head
let node = head
for(let i = 0; i<k;i++){
if(node === null){
return head;
}
node = node.next;
}
for(let j = 0;j<k;j++){
let temp = current.next;//这里用temp保存一下
current.next = pre;
pre = current;
current = temp;
}
head.next = reverseKGroup(current,k)
return pre
}
module.exports = {
reverseKGroup : reverseKGroup
};