C++ Easy Approach O(n) Time, and O(1) Space;


#1
ListNode* Solution::swapPairs(ListNode* A) {
    if(A->next == NULL)
    {
        return A;
    }
    ListNode* ans = A->next;
    ListNode* head = ans;
    ListNode* prev = A;
    while(head->next && head->next->next)
    {
        ListNode* move = head->next->next;
        ListNode* temp = head->next;
        head->next = prev;
        prev->next = move;
        prev = temp;
        head = move;
    }
    ListNode* temp = head->next;
    head->next = prev;
    prev->next = temp;
    return ans;
}