Wrong answer in one test case

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;
}

please reply as early as possible

Hey @sawan_verma
your approach to solve this question is wrong. for finding the intersection point, you don’t need to compare the magnitudes of a->data and b->data, intersection doesn’t depend on this
there can be several approaches to solve this question, the simplest one would be:

Use 2 nested for loops. The outer loop will be for each node of the 1st list and inner loop will be for 2nd list. In the inner loop, check if any of nodes of the 2nd list is same as the current node of the first linked list. The time complexity of this method will be O(M * N) where m and n are the numbers of nodes in two lists.

Try writing your code using this logic.