Swap Nodes in Pairs, C++
ListNode* swapPairs(ListNode* head) {        
        if (head != nullptr && head->next != nullptr) {
            ListNode* second = head;
            ListNode* first = head->next;
            
            // second->next to third 
            second->next = head->next->next;
			
            // first->next to second
            first->next = second;
            
            // second->next to swapped 3 <-> 4 
            second->next = swapPairs (second->next);
            
            return first;
        }
        return head;
    }
Comments (0)