Straightforward C++ Solution


  • 0

    First put all these nodes in a vector, and then start the connecting work from both ends.

    class Solution {
    public:
        void reorderList(ListNode* head) {
            vector<ListNode*> v;
            ListNode* tmp = head ;
            while( tmp ) {
                v.push_back(tmp);
                tmp = tmp->next;
            }       
            for( int i=0, j=v.size()-1; i<=j; i++, j-- ) {
                if( tmp ) tmp->next = v[i];
                v[i]->next = v[j];
                tmp = v[j];
            }
            if( tmp ) tmp->next = NULL;
        }
    };

Log in to reply
 

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