C++ 20ms solution


  • 0
    class Solution {
    public:
    ListNode* oddEvenList(ListNode* head) {
        if (head==NULL || head->next==NULL || head->next->next==NULL) return head;
        ListNode *evenHead,*curOdd,*curEven;
        evenHead=head->next;
        curOdd=head,curEven=evenHead;
        while (curEven && curEven->next){
            curOdd->next = curEven->next;
            curOdd=curOdd->next;
            curEven->next = curOdd->next;
            curEven = curEven->next;
        }
        curOdd->next = evenHead;
        return head;
    }
    };

Log in to reply
 

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