My simple c++ solution


  • 0
    K
    class Solution {
    public:
    ListNode* oddEvenList(ListNode* head) {
        if(!head || !head->next ||!head->next->next)return head;
        ListNode * odd=head;
        ListNode * even=head->next;
        ListNode * tmp=even;
        while(even||odd)
        {
            odd->next=odd->next->next;
            even->next=even->next->next;
            odd=odd->next; 
            even=even->next;
            if(odd->next==NULL || odd->next->next==NULL)break;
            
        }
       odd->next=tmp;
        return head;
    }
    

    };


  • 1
    D
    class Solution {
    public:
        ListNode* oddEvenList(ListNode* head) {
            if(!head){return head;}
            ListNode* odd = head, *even = head->next, *evenHead = even;
            while(even && 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.