Swapping Nodes in Pairs using Recursion

C++ Solution using Recursion

class Solution 
{
public:
    ListNode* swapPairs(ListNode* head)
    {
        if(head==NULL || head->next==NULL)
            return head;
        
        ListNode *first = head->next;
		ListNode *second = head->next->next;
        first->next = head;
        head->next = swapPairs(second);          //Recursive Call
        
        return first;
    }     
};
Comments (2)