Any problems in my c++ code? Runtime error


  • 0
    I
    ListNode *insertionSortList(ListNode *head) {
        if(head == NULL)
            return head;
        ListNode *pre = head;
        ListNode *p = head->next;
        ListNode *q, *tmp;
        while(p != NULL)
        {
            while(p != NULL && p->val > pre->val)
            {
                pre = p;
                p = p->next;
            }
            if(p->next == NULL)
                break;
            else
            {
                q = p->next;
                if(head->val > p->val)
                {
                    p->next = head;
                    head = p;
                }
                else
                {
                    tmp = head;
                    while(tmp != pre && tmp->val < p->val)
                        tmp = tmp->next;
                    tmp->next = p;
                    pre = p;
                }
                p = q;
            }
        }
        return head;
    }
    

    Submission Result: Runtime Error

    Last executed input:	{3,2,4}

  • 0
    J

    You did not check p != NULL in if(p->next == NULL).


Log in to reply
 

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