jayati

Pairwise swap elements of a linked list

Sep 6th, 2023
1,704
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.07 KB | None | 0 0
  1. //{ Driver Code Starts
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. struct Node
  6. {
  7.     int data;
  8.     struct Node* next;
  9.    
  10.     Node(int x){
  11.         data = x;
  12.         next = NULL;
  13.     }
  14. };
  15.  
  16.  
  17. // } Driver Code Ends
  18. /*
  19.   Pairwise swap a linked list
  20.   The input list will have at least one element
  21.   node is defined as
  22.  
  23. struct Node
  24. {
  25.     int data;
  26.     struct Node* next;
  27.  
  28.     Node(int x){
  29.         data = x;
  30.         next = NULL;
  31.     }
  32.  
  33. }*head;
  34. */
  35. class Solution
  36. {
  37.     public:
  38.     Node* pairWiseSwap(struct Node* head)
  39.     {
  40.        if (head == nullptr || head->next == nullptr) {
  41.         return head;
  42.     }
  43.     Node* prev = nullptr;
  44.     Node* curr = head;
  45.     Node* forward = nullptr;
  46.     while (curr != nullptr && curr->next != nullptr) {
  47.         forward = curr->next;
  48.         if (prev == nullptr) {
  49.             head = forward;
  50.         } else {
  51.             prev->next = forward;
  52.         }
  53.         curr->next = forward->next;
  54.         forward->next = curr;
  55.         prev = curr;
  56.         curr = curr->next ;
  57.     }
  58.    
  59.     return head;
  60.      
  61.     }
  62. };
  63.  
  64. //{ Driver Code Starts.
  65.  
  66. void printList(Node* node)
  67. {
  68.     while (node != NULL) {
  69.         cout << node->data <<" ";
  70.         node = node->next;
  71.     }  
  72.     cout<<"\n";
  73. }
  74.  
  75. int main()
  76. {
  77.     int t;
  78.     cin>>t;
  79.     while(t--)
  80.     {
  81.         int n;
  82.         cin>>n;
  83.  
  84.         int data;
  85.         cin>>data;
  86.         struct Node *head = new Node(data);
  87.         struct Node *tail = head;
  88.         map<Node*, int> mp;
  89.         mp[head] = head->data;
  90.         for (int i = 0; i<n-1; ++i)
  91.         {
  92.             cin>>data;
  93.             tail->next = new Node(data);
  94.             tail = tail->next;
  95.             mp[tail] = tail->data;
  96.         }
  97.         struct Node *failure = new Node(-1);
  98.         Solution ob;
  99.         head = ob.pairWiseSwap(head);
  100.         int flag = 0;
  101.         struct Node *temp = head;
  102.         while(temp){
  103.             if(mp[temp] != temp->data){
  104.                 flag = 1;
  105.                 break;
  106.             }
  107.             temp = temp->next;
  108.         }
  109.         if(flag)
  110.             printList(failure);
  111.         else
  112.             printList(head);
  113.     }
  114.     return 0;
  115. }
  116.  
  117. // } Driver Code Ends
  118. // https://practice.geeksforgeeks.org/problems/pairwise-swap-elements-of-a-linked-list-by-swapping-data/
Advertisement
Add Comment
Please, Sign In to add comment