What is wrong with this code? Please help


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

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

curr->next = prev;

prev->next = swapPairs(fut);

return curr;



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!