Why switching two lines cause the codes not accepted?


  • 0
    B

    I have the following codes accepted. However, when I switch the order of following two lines:

    i = i->next;
    h->next = tmp;
    

    the codes get a running time error. Namely, if we execute

    h->next = tmp;
    

    before

    i = i-> next;
    

    we would have running time exceed error. Any idea why?

    ListNode* reverseBetween(ListNode* head, int m, int n) {
        ListNode * h;
        ListNode * t;
        
        ListNode* pre;
        
        int count = 1;
        ListNode* i = head;
        while (count<=n){
            if (m-1 > 0 && count == m-1){
                pre = i;
                i = i->next;
            }
            else if (count==m){
                h = i;
                t = i;
                i = i->next;
            }
            else if (count > m){
                ListNode* tmp = h;
                h = i;
                i = i->next;
                h->next = tmp;
            }
            else{
                i = i->next;
            }
            count++;
        }
        
        if (m-1>0){
            pre->next = h;
        }
        else{
            head = h;
        }
        t->next = i;
        
        return head;
        
    }

Log in to reply
 

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