Easy CPP solution 0(n)


#1

ListNode* Solution::swapPairs(ListNode* head) {
if( head ==NULL) return NULL;
struct ListNode *curr = head->next;
struct ListNode *prev= head;
if( curr ==NULL ) return head;
while( curr->next && curr->next->next){
swap( curr->val, prev->val);
struct ListNode *temp= curr->next ;
prev= temp;
curr= prev->next;

    }
    if( curr && prev ) swap(curr->val, prev->val );
    return head;

}