Why teat case 1 failed

#include
using namespace std;
struct node{
int data;
struct node *next;
};
struct node *p;
void addtrail(node &head,int data){
node
n=new node;
n->data=data;
n->next=NULL;
if(head==NULL){

head=n;
p=n;
}
p->next=n;
p=n;

}
void merge(node *&head,int n,node *&head1,int m){
if(n>m){
while(n!=m){
head=head->next;
n=n-1;
}
}
if(n<m){
while(n!=m){
head1=head1->next;
m=m-1;
}
}
while(head&&head1){
if((head->data)==(head1->data)){
cout<data;
break;
}
head=head->next;
head1=head1->next;
}
if(head==NULL&&head1==NULL){
cout<<-1;
}

}
int main() {
int n;
cin>>n;
int a[n];
node *head=NULL;
for(int i=0;i<n;i++){
cin>>a[i];
addtrail(head,a[i]);
}

int m;
cin>>m;
int b[m];
node *head1=NULL;
for(int j=0;j<m;j++){
    cin>>b[j];
    addtrail(head1,b[j]);
}
merge(head,n,head1,m);
    
    



return 0;

}

@dineshjani hey dinesh jani please share your code through ide.codingblcoks.com just copy your code on this link and press the save button it will generate a link that link you have to post here do it asap to resolve your doubt asao.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.