Why my solution gets TLE?


  • 0
    Z

    I got TLE one the {3,5} 1 2 case.
    But it runs good on my own machine.
    Does anyone know what happens?

    ListNode *reverseBetween(ListNode *head, int m, int n) {
        if(m==n)return head;
        ListNode *hh=new ListNode(0),*end=head,*l=head,*r=head;
        hh->next=head;
        for(int i=0;i<n-m;i++)end=end->next;
        for(int i=1;i<m;i++){
            hh=hh->next;
            end=end->next;
            l=l->next;
            r=r->next;
        }
       /*end point to the nth + 1 node, 
        l and r point to the mth node,
        hh point to the mth - 1 node;*/
    
    
        for(int i=0;i<n-m;i++){
            r=l->next;
            l->next=end;
            hh->next=r;
            end=l;
            l=r;
            
        }
       /*move the left node to the end for each time.*/
    
        l->next=end;//link the last two node;
    
        if(m==1)return hh->next;
        return head;
    }

Log in to reply
 

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