Sharing my C++ neat solution


  • 0
    B
    class Solution {
    public:
        ListNode* oddEvenList(ListNode* head) {
            if(!head || !head->next) return head;
            ListNode *ho = head, *he = head->next, *run = head;
            int ctr = 0;
            while(run->next) {
                ctr = ctr^1;
                ListNode* tmp = run->next;
                if(!run->next->next) {
                    if(ctr == 1) run->next = he;
                    else {
                        run->next = nullptr;
                        tmp->next = he;
                    }
                    break;
                } else {
                    run->next = run->next->next;
                    run = tmp;
                }
            }
            return head;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.