Write your pattern overview here...
💡 Use two pointers moving towards each other or in the same direction based on the problem.
1// Your code here2#include <iostream>34struct Node {5 int data;6 Node* next;7 Node(int val) : data(val), next(nullptr) {}8};910// Function to swap nodes with values x and y11void swapNodes(Node*& head, int x, int y) {12 // Nothing to do if values are the same13 if (x == y) return;1415 Node *prevX = nullptr, *currX = head;16 while (currX && currX->data != x) {17 prevX = currX;18 currX = currX->next;19 }2021 Node *prevY = nullptr, *currY = head;22 while (currY && currY->data != y) {23 prevY = currY;24 currY = currY->next;25 }2627 // If either x or y is not present, nothing to do28 if (!currX || !currY) return;2930 // If x is not head of linked list, make prevX point to y31 if (prevX != nullptr)32 prevX->next = currY;33 else // Else make y the new head34 head = currY;3536 // If y is not head of linked list, make prevY point to x37 if (prevY != nullptr)38 prevY->next = currX;39 else // Else make x the new head40 head = currX;4142 // Swap next pointers of x and y43 Node* temp = currY->next;44 currY->next = currX->next;45 currX->next = temp;46}