52 ms C++ ,accepted, beat more than 80%


  • 0
    F
    void reorderList(ListNode* head) {
        if (!head || !head->next || !head->next->next) return;
        vector<ListNode*> v;
        ListNode* temp = head; 
        while(temp){
            v.push_back(temp);
            temp = temp->next;
        }
        int n = v.size();
        int first = 1; int last = n-1;
        int count = 0; // flag 
        while(first <= last){
            if(count == 0){
                head->next = v[last];
                head = head->next;
                last--;
                count = 1;
            }
            else if(count == 1){
                head->next = v[first];
                head = head->next;
                first++;
                count = 0;
            }
        }
        head->next = NULL;
    }

Log in to reply
 

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