题解 | #链表中的节点每k个一组翻转#
链表中的节点每k个一组翻转
https://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 var cur = head st =[] re =[] var counter = 1 while(cur){ if(counter%k != 0){ st.push(cur.val) cur = cur.next counter++ }else{ st.push(cur.val) cur = cur.next counter++ for(let i =0; i<k; i++){ res = st.pop() re.push(res) } } } var cur =head; for(let i=0; i<re.length; i++){ cur.val = re[i] cur = cur.next } return head; } module.exports = { reverseKGroup : reverseKGroup };