홀수 짝수 목록 - I

2462 단어 leetcodejavascript
var oddEvenList = function(head) {
    if (!head || !head.next) return head;
    let oddNode = head;
    const evenHead = head.next;
    let evenNode = head.next;

    while(evenNode?.next) {
        oddNode.next = evenNode.next;
        oddNode = oddNode.next;
        evenNode.next = oddNode.next;
        evenNode = evenNode.next;
    }

    oddNode.next = evenHead;

    return head;
};

좋은 웹페이지 즐겨찾기