C++ recursive solution


  • 0
    F
    class Solution {
    public:
       void reorderList(ListNode *head) {
          _p1 = head; walk(head);
       }
    private:
       ListNode *_p1;
       void walk(ListNode *p) {
          if( p ) {
             walk(p->next);
             if( ! _p1->next ) return;
             if( _p1==p || _p1==_p1->next ) _p1->next = NULL;
             else {
                auto p1n = _p1->next;
                _p1->next = p;
                p->next = _p1 = p1n;
             }
          }
       }
    };
    

Log in to reply
 

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