Intersection Point of two linked lists

I am confused about the return statement on line 74. some of the test cases are showing TLE. https://ide.codingblocks.com/s/299163

Aastha, instead of returning the node, you can simply return the data…
You can refer to this function as :

int intersection(nodehead1,nodehead2,int N1,int N2)
{
int len1=N1;
int len2=N2;
int diff;
if(len2>len1)
{
diff=len2-len1;
}
else
{
diff=len1-len2;
}
if(len2>len1)
{
for(int i=0;i<diff;i++)
{
head2=head2->next;
}
}
else
{
for(int i=0;i<diff;i++)
{
head1=head1->next;
}
}
while(head1 != NULL && head2 != NULL)
{
if(head1->data != head2->data)
{
head1 = head1->next;
head2 = head2->next;
}
else if(head1->data==head2->data)
{
return head1->data;
}
}
return -1;
}

I am getting a compilation error. Please check the code.

Aastha, the method you are trying to use to build the linked list is not correct… Pls use the native approach for the linked list, through the buildlist and insertAtTail function :

void insertAtTail(node *&head,int data)
{

if(head==NULL)
{
    head = new node(data);
    return;
}
node*tail = head;
while(tail->next!=NULL)
{
    tail = tail->next;
}
tail->next = new node(data);
return;

}

void buildlist(node*&head,int n)
{
int data;
while(n–)
{
cin>>data;
insertAtTail(head,data);
}
}