Complete-Preparation

πŸŽ‰ One-stop destination for all your technical interview Preparation πŸŽ‰

View the Project on GitHub

24. Swap Nodes in Pairs 🌟🌟

Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list’s nodes (i.e., only nodes themselves may be changed.)

By swapping values

class Solution {
private:
    ListNode* swapPairsRec(ListNode* head) {
        if(head==NULL||head->next==NULL) return head;
        swap(head->val,head->next->val);
        swapPairsRec(head->next->next);
        return head;
    }
public:
    ListNode* swapPairs(ListNode* head) {
        return swapPairsRec(head);
    }
};
class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        if(head==NULL||head->next==NULL) return head;
        ListNode* temp = head;
        while(head!=NULL && head->next!=NULL){
            swap(head->val,head->next->val);
            head = head->next->next;
        }
        return temp;
    }
};

By swapping Nodes

class Solution {
public:
    ListNode* swapPairs(ListNode* head)
    {
        if (!head || !head->next) {
            return head;
        }

        ListNode* temp;
        temp = head->next;

        head->next = swapPairs(head->next->next); // change links
        temp->next = head;

        return temp; // now after changing links, temp act as our head
    }
};
class Solution {
public:
    ListNode* swapPairs(ListNode* head)
    {
        if (!head || !head->next)
            return head;

        ListNode* temp = new ListNode();
        ListNode *prev = temp, *curr = head;

        while (curr && curr->next) {
            // swap
            prev->next = curr->next;
            curr->next = prev->next->next;
            prev->next->next = curr;

            // update the pointers
            prev = curr;
            curr = curr->next;
        }

        return temp->next;
    }
};