Run time Error !


  • 0
    D

    class Solution {
    public:

    ListNode *findmid(ListNode *head) {
        ListNode *sp=head,*fp=head;
        int f=0;
        while(fp->next!=NULL)
        {
            if(f==0)
            {
                fp=fp->next;
                f=1;
            }
            else
            {
                
                fp=fp->next;
                sp=sp->next;
                f=0;
            }
        }
        return sp;
        
    }
    ListNode *merge(ListNode *l1,ListNode *l2) 
    {
        ListNode *new1,*t;
        int f=0;
        if(l1==NULL)
        return l2;
        if(l2==NULL)
        return l1;
    while(l1!=NULL&&l2!=NULL)
       {
           if(l1->val<=l2->val)
           {
               if(f==0)
               {new1=l1;t=new1;f=1;}
               else
               {t->next=l1;t=t->next;}
               l1=l1->next;
           }
           else
           {
               
               if(f==0)
               {new1=l2;t=new1;f=1;}
               else
               {t->next=l2;t=t->next;}
               l2=l2->next;
           }
       }
       while(l1!=NULL)
       {t->next=l1;t=t->next;l1=l1->next;}
       while(l2!=NULL)
       {
           t->next=l2;t=t->next;l2=l2->next;
       }
       return new1;
    }
    ListNode *sortList(ListNode *head) {
        if(head==NULL)
        return head;
        else{
        ListNode *l1=NULL,*l2=NULL,*mid;
        mid=findmid(head);
        if(mid->next!=NULL){
        l2=mid->next;
        mid->next=NULL;}
        l1=head;
        l1=sortList(l1);
        l2=sortList(l2);
        head=merge(l1,l2);
        return head;
        }
    }
    

    };


Log in to reply
 

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