What is wrong with output?


  • 0
    S
    void reorderList(ListNode *head)
    {
        if (head == NULL)
            return;
        stack<ListNode *> s;
        ListNode *fast = head;
        ListNode *slow = head;
    
        while (fast != NULL && fast->next != NULL)
        {
            fast = fast->next->next;
            slow = slow->next;
        }
    
        ListNode *p = slow->next;
        slow->next = NULL;
    
        while (p != NULL)
        {
            s.push(p);
            p = p->next;
        }
    
        ListNode *q = head->next;
        while (q != NULL)
        {
            if (s.empty())
                return;
            ListNode *t = s.top();
            s.pop();
            t->next = q->next;
            q->next = t;
            q = q->next->next;
        }
    }

Log in to reply
 

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