BM14 链表的奇偶重排
/*
* 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 curr = head;
let even = head.next;
let next = even;
// 记录 curr 绕过偶数只记录奇数
// next 只记录偶数
while (next && next.next) {
curr.next = next.next;
curr = curr.next;
next.next = curr.next;
next = next.next;
}
// 将奇数和偶数合并
curr.next = even;
return head;
}
module.exports = {
oddEvenList: oddEvenList,
};
如有问题望指正

京公网安备 11010502036488号