C++ solution, 13ms


  • 0
    J

    Here I use 3 pointers, pre, cur, and move. In a listed that has been move some steps, these pointers represent will be: *pre at 5,*cur at 6, move *at 7.
    1,3,5,2,4,6,7,8

    class Solution {
    public:
        ListNode* oddEvenList(ListNode* head) {
            if(head==NULL) return head;
            ListNode *pre, *cur, *move;
            pre=head;
            cur=head->next;
            while(cur && cur->next){
                move=cur->next;
                // move the odd nodes before hte even nodes
                cur->next=move->next;
                move->next=pre->next;
                pre->next=move;
                //move to next nodes
                pre=pre->next;
                cur=cur->next;
            }
            return head;
        }
    };
    

Log in to reply
 

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