Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //{ Driver Code Starts
- #include <bits/stdc++.h>
- using namespace std;
- struct Node
- {
- int data;
- struct Node* next;
- Node(int x){
- data = x;
- next = NULL;
- }
- };
- // } Driver Code Ends
- /*
- Pairwise swap a linked list
- The input list will have at least one element
- node is defined as
- struct Node
- {
- int data;
- struct Node* next;
- Node(int x){
- data = x;
- next = NULL;
- }
- }*head;
- */
- class Solution
- {
- public:
- Node* pairWiseSwap(struct Node* head)
- {
- if (head == nullptr || head->next == nullptr) {
- return head;
- }
- Node* prev = nullptr;
- Node* curr = head;
- Node* forward = nullptr;
- while (curr != nullptr && curr->next != nullptr) {
- forward = curr->next;
- if (prev == nullptr) {
- head = forward;
- } else {
- prev->next = forward;
- }
- curr->next = forward->next;
- forward->next = curr;
- prev = curr;
- curr = curr->next ;
- }
- return head;
- }
- };
- //{ Driver Code Starts.
- void printList(Node* node)
- {
- while (node != NULL) {
- cout << node->data <<" ";
- node = node->next;
- }
- cout<<"\n";
- }
- int main()
- {
- int t;
- cin>>t;
- while(t--)
- {
- int n;
- cin>>n;
- int data;
- cin>>data;
- struct Node *head = new Node(data);
- struct Node *tail = head;
- map<Node*, int> mp;
- mp[head] = head->data;
- for (int i = 0; i<n-1; ++i)
- {
- cin>>data;
- tail->next = new Node(data);
- tail = tail->next;
- mp[tail] = tail->data;
- }
- struct Node *failure = new Node(-1);
- Solution ob;
- head = ob.pairWiseSwap(head);
- int flag = 0;
- struct Node *temp = head;
- while(temp){
- if(mp[temp] != temp->data){
- flag = 1;
- break;
- }
- temp = temp->next;
- }
- if(flag)
- printList(failure);
- else
- printList(head);
- }
- return 0;
- }
- // } Driver Code Ends
- // https://practice.geeksforgeeks.org/problems/pairwise-swap-elements-of-a-linked-list-by-swapping-data/
Advertisement
Add Comment
Please, Sign In to add comment