Sharing my 20MS Solution


  • 0
    C
    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        ListNode* oddEvenList(ListNode* head) {
            int cnt=1;
            if(head==NULL) return NULL;
            ListNode* odd=new ListNode(0),*OL = odd;
            ListNode* even=new ListNode(0),*EL=even;
            while(head){
                odd->next= head;
                odd = odd->next;
                head=head->next;
                odd->next=NULL;
                if(head!=NULL) {
                    even->next=head;
                    even=even->next;
                    head=head->next;
                    even->next=NULL;
                }
            }
            odd->next = EL->next;
            return OL->next;
        }
    };

Log in to reply
 

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