Swap nodes in pair of a linked List using Recursion.

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

    if(head == NULL || head->next == NULL)
    { return head;
    }  
    int tmp = head->val;
    head->val = head->next->val ; 
    head->next->val = tmp;
    
    swapPairs(head->next->next);
    
   return head; 
}

};

Comments (0)