奇偶链表
描述:
题解:
这个问题最直接的想法就是双指针思想来构建奇偶链表,然后把偶链表连接在奇链表的尾部。
ES6
const oddEvenList = (head) => {
if (head == null) {
return null
}
let odd = head, even = head.next, evenHead = even;
while (even != null && even.next != null) {
odd.next = even.next
odd = odd.next
even.next = odd.next
even = even.next
}
odd.next = evenHead
return head
};
时间复杂度:O(n)