Improvement in linklist intersection point program

Clash Royale CLAN TAG#URR8PPPImprovement in linklist intersection point program
I have written below code for finding intersection point of linklist.
Can somebody please review the same and tell me is there any improvement I can do to make it better.
Algo-:
If at any point p1 meets p2, then p1/p2 is the intersection node.
int getIntesectionNode(struct Node* head1, struct Node* head2)
{
struct Node *start1 = head1;
struct Node *start2 = head2;
bool endFound1 = false;
bool endFound2 = false;
if( start1 == NULL || start2 == NULL)
{
return -1;
}
while(1)
{
start1 = start1->next;
start2 = start2->next;
if( start1 != start2)
{
if( start1 == NULL)
{
if (endFound1)
{
printf("Intersection not found !");
break;
}
start1 = head2;
endFound1 = true;
}
if( start2 == NULL)
{
if (endFound2 )
{
printf("Intersection not found !");
break;
}
start2 = head1;
endFound2 = true;
}
}
else
{
printf("Intersection point foundn");
printf("%d",start1->data);
return start1->data;
}
}
return -1;
}
1 Answer
1
I think I don't understand, but if you are doing what I think, I would use a second loop,
and check all of LL-1 foreach LL-2. I would also check the starting point for equality
before starting the loop and going to the next point, without checking it until you loop around.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
What is your question? Does your code work? If not, what errors do you get? Or how does the output differ from what you want?
– Code-Apprentice
5 mins ago