1//not changing the data inside
2//Try on your own first
3#include<iostream>
4using namespace std;
5struct node
6{
7 int data;
8 node* next;
9};
10node * head=NULL;
11void insertion(int key)
12{
13 node *temp=new node;
14 temp->data=key;
15 temp->next=NULL;
16 if(head==NULL)
17 {
18 head=temp;
19 }
20 else
21 {
22 node* ptr=head;
23 while(ptr->next!=NULL)
24 {
25 ptr=ptr->next;
26 }
27 ptr->next=temp;
28 }
29}
30void swapnode(int x,int y)
31{
32 if(x==y)
33 {
34 return;
35 }
36 node *x_prev=NULL;
37 node* x_curr=head;
38 node* y_prev=NULL;
39 node* y_curr=head;
40 while(x_curr!=NULL&&x_curr->data!=x)
41 {
42 x_prev=x_curr;
43 x_curr=x_curr->next;
44 }
45 while(y_curr!=NULL&&y_curr->data!=y)
46 {
47 y_prev=y_curr;
48 y_curr=y_curr->next;
49 }
50 if(x_curr==NULL||y_curr==NULL)
51 {
52 return;
53 }
54 if(x_prev!=NULL)
55 {
56 x_prev->next=y_curr;
57 }
58 else
59 {
60 head=y_curr;
61 }
62 if(y_prev!=NULL)
63 {
64 y_prev->next=x_curr;
65 }
66 else
67 {
68 head=x_curr;
69 }
70 node *temp=y_curr->next;
71 y_curr->next=x_curr->next;
72 x_curr->next=temp;
73}
74void print()
75{
76 node* temp=head;
77 while(temp!=NULL)
78 {
79 cout<<temp->data<<" ";
80 temp=temp->next;
81 }
82}
83int main()
84{
85 int n;
86 cin>>n;
87 int value;
88 for(int i=0;i<n;i++)
89 {
90 cin>>value;
91 insertion(value);
92 }
93 int p,q;
94 cin>>p>>q;
95 swapnode(p,q);
96 print();
97 return 0;
98}
99