DEV Community

ZeeshanAli-0704
ZeeshanAli-0704

Posted on

Odd Even List - I

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;
};

Enter fullscreen mode Exit fullscreen mode

Top comments (0)