What is wrong with this code? Please help


#1

ListNode* Solution::swapPairs(ListNode* A) {

if(A->next == NULL)
    return A;
    
if(A==NULL)
    return NULL;
    
ListNode* prev = A;
ListNode* curr = A->next;
ListNode* fut = curr->next;

curr->next = prev;

prev->next = swapPairs(fut);

return curr;

}


#2

You are checking A->next==NULL even before checking A==NULL. In case A is NULL checking A->next gives you out of bound error. Just check A==NULL first , then A->next==NULL and your code works fine!