code showing wrong answer in a test case
my code-----
#include
using namespace std;
class node
{
public:
int data;
node* next;
//constructor
node(int d){
data=d;
next=NULL;
}
};
void insertAtTail(node*&head,int data){
if(head==NULL){
head=new node(data);
return;
}
node* temp=head;
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=new node(data);
return;
}
node* buildlist(node*& head,int n){
while(n–)
{
int data;
cin>>data;
insertAtTail(head,data);
}
}
void print(node* head){
node* temp=head;
while(temp!=NULL){
cout<data<<" ";
temp=temp->next;
}
}
int intersect_point(node* a,node* b){
if(a == NULL || b == NULL){
return -1;
}
if(a->data==b->data)
return a->data;
if(a->data>b->data){
return intersect_point(a,b->next);
}
else{
return intersect_point(a->next,b);
}
}
int main() {
int n1;
cin>>n1;
node* a=NULL;
buildlist(a,n1);
int n2;
cin>>n2;
node* b=NULL;
buildlist(b,n2);
cout<<intersect_point(a,b)<<endl;
// print(a);
//print(b);
return 0;
}