题解 | #链表的奇偶重排#
链表的奇偶重排
https://www.nowcoder.com/practice/02bf49ea45cd486daa031614f9bd6fc3
/* * function ListNode(x){ * this.val = x; * this.next = null; * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @return ListNode类 */ function oddEvenList(head) { // write code here if (!head) return head; let cur = head, count = 0, flag = null while (cur.next && cur.next.next) { count++ console.log(cur) let even = cur.next cur.next = cur.next.next even.next = even.next.next cur = cur.next if(count == 1) { flag = even } } if(flag !== null) { cur.next = flag } return head } module.exports = { oddEvenList: oddEvenList, };