How to correct this error


  • 0
    A
    class Solution
    {
    public:
    ListNode *reverseBetween(ListNode *head, int m, int n)
    {
    
        ListNode *a = head , *b = head, *link = NULL;
        int i = 1, j = 1;
    
        while (i != m || j != n)
        {
            if (i != m)
            {
                link = a;
                a = a->next;
                i++;
            }
            if (j != n)
            {
                b = b->next;
                j++;
            }
    
        }
        
    
        ListNode *prev = b->next , *curr = a , *temp;
        while (curr != b)
        {
            temp = curr->next;
            curr->next = prev;
            prev = curr;
            curr = temp;
    
        }
        curr->next = prev;
        if (link) link->next = curr; //check if Link is NULL
        else head = curr;  /* This Line causes error . It changes list here but after the function is returned 
                                       I get the list starting from the old head node .
                              Ex : (3,5) m = 1 n = 2
                              Modified list inside fn when printed prints 5 3 
                              but outside the fn it prints 3 5 
                              Even though I am dealing with pointers , why is this local error creeping up?
    
    
    
    }
    

    };


Log in to reply
 

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