Why runtime error


  • 0
    J
    ListNode *insertionSortList(ListNode *head) {
        if(head==NULL) return head;
        ListNode *p = head;
        int i = 1,j;
        while(p->next!=NULL){
            ListNode *q2 = head,*q1=NULL;
            for(j=0;j<i&&q2!=NULL;j++){
                if(p->next->val<q2->val){
                    ListNode *tmp = p->next;
                    p->next = p->next->next;
                    tmp->next = q2;
                    if(q1==NULL) head = tmp;
                    if(q1!=NULL) q1->next = tmp;
                    break;
                }
                q1 = q2;
                q2 = q2->next;
            }
            p = p->next;
            i++;
        }
        return head;
    }

  • 0
    W

    I encountered the same situation...


  • 0
    W

    I've solved my problem..but it's not the case as yours...and I guess your problem lies "p->next->next"


Log in to reply
 

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