What is wrong with my code?(Wrong answer)


  • 0
    K

    class Solution {
    public:
    ListNode *swapPairs(ListNode *head) {

        if (head == NULL || head -> next == NULL) return head;
        
        ListNode *pCur, *pNext;
        
        pCur = head -> next;
        pNext = NULL;
        
        if (pCur -> next != NULL) head -> next = pCur -> next;
        
        while (pCur != NULL && pCur -> next != NULL)
        {
            pNext = pCur -> next -> next;
            pCur -> next -> next = pCur;
            pCur -> next = pNext;
            pCur = pNext;
        }
        
        return head;
    }
    

    };


Log in to reply
 

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