Run time error. What is wrong with the code?


  • 0
    G

    class Solution {
    public:
    ListNode *insertionSortList(ListNode *head) {

        if(head==NULL){
            return head;
        }
    
        ListNode *m=head;
        ListNode *m1=head->next;
        while(m1!=NULL){
    
            if(head->val>m1->val){
                m->next=m1->next;
                m1->next=head;
                head=m1;
            }
             m=m->next;
             m1=m1->next;
        }
    
        ListNode *a1=head;
        ListNode *a2=head->next;
        while(a2!=NULL){
            m1=a2;
            ListNode *m2=a2->next;
            while(m2!=NULL){
                if(a2->val>m2->val){
                    m1->next=m2->next;
                    a1->next=m2;
                    m2->next=a2;
                   }
              
                m1=m1->next;
                m2=m2->next;
            }
            a1=a1->next;
            a2=a2->next;
    
        }
        return head;
    
    }
    

    };


  • 0
    H

    If "m1->next=head;" and "head=m1;"
    then
    "m1->next" is m1.

    Why do you want a circular list?


Log in to reply
 

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