c++ recursive method


  • 0
    J
    class Solution {
    public:
        void reorderList(ListNode* head) {
            
            
            if(head == NULL || head->next == NULL)
                return;
            ListNode* cur = head;
            head = helper(cur);
        }
        ListNode* helper(ListNode* head)
        {
            
            if(head == NULL || head->next == NULL)
                return head;
            ListNode* tail = head;
            ListNode* cur = head;
            ListNode* pre=head;
            while(tail->next != NULL)
            {
                pre = tail;
                tail = tail->next;
            }
            pre->next = NULL;
            ListNode* nxt = cur->next;
            cur->next = tail;
            tail->next = helper(nxt);
            return head;
            
        }
    };
    

Log in to reply
 

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