Can anyone tell me why this code give runtime error?


  • -1
    M

    class Solution {

    public:

    ListNode* start = NULL;
    void reorderList(ListNode* head) {
        if(head != NULL){
            if(start == NULL)
                start = head;
            reorderList(head->next);
            if(start == head || start->next == head)
                head->next = NULL;
            else{
                ListNode* tmp = start->next;
                start->next = head;
                head->next = tmp;
                start = tmp;
            }
        }
    }
    

    };


  • 0
    X

    After you reached this stageL: "
    if(start == head || start->next == head)
    head->next = NULL;"
    The recursion shall stop and return;


Log in to reply
 

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