Inplace O(n) solution C++


  • 0
    S
    public:
        ListNode* oddEvenList(ListNode* head) {
            ListNode * odd = head;
            ListNode* even = odd?odd->next:NULL;
            ListNode* evenhead = even;
            if(even == NULL) return head;
            while(odd->next && even->next){
                odd->next = even->next;
                odd = odd->next;
                even->next = odd->next;
                even = even->next;
            }
            odd->next = evenhead;
            return head;
        }

Log in to reply
 

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