Simple C++ solution


  • 0
    M
    class Solution {
    public:
        ListNode* oddEvenList(ListNode* head) {
            
            if(head == NULL)
                return NULL;
            
            if(head->next == NULL)
                return head;
           
           ListNode * oddH = head;
           ListNode * evenH = head->next;
           
           ListNode * odd = oddH;
           ListNode * even = evenH;
           
           while(odd && even && even->next)
           {
             odd->next = even->next;
             odd = odd->next;
             
             even->next = odd->next;
             even = even->next;
           }
           
           odd->next = evenH;
           
           return oddH;
        }
    };

Log in to reply
 

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