My easy C++ solution


  • 4
    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)//1->1->NULL
        {
            odd->next=odd->next->next;//1->3->5
            even->next=even->next->next;//2->4->null
            
            odd=odd->next; //3
            even=even->next;//4
            if(odd->next==NULL || odd->next->next==NULL)break;
            
        }
       odd->next=tmp;
        return head;
       }
    };

Log in to reply
 

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