Intersection point of two linked lists || giving run error

Given two intersecting linked lists, write a function to find its point of intersection. If the lists do not intersect , return NULL.

Note : You are required to only write a single function. Do not modify / alter the remaining code.

This is a stub problem so you need not worry about taking input or displaying output. Only focus on the designated function.

Input Format
You are given a function which accepts head pointers of two linked lists.

Constraints
Your function should run in linear time.

Output Format
Return the intersection point node.

Sample Input
Consider these linked lists :
1 -> 2 -> 3 -> null
:arrow_upper_right:
4

(This is not the actual input that will be provided but rather a description of it)
Sample Output
The two linked lists 1->2->3->null and 4->2->3-> null intersect at node with data 2.
Return the node with data = 2.
Explanation
The two linked lists intersect at 2. Return their intersection point.

Q- https://online.codingblocks.com/app/player/74030/content/128054/5166/code-challenge

code- https://ide.codingblocks.com/s/347945

hello @anubhavb0011

image
dont compare data ,compare their addresses
while(l1 !=NULL && l2!=NULL && l1!=l2){
** l1=l1->next;**
** l2=l2->next;**
}

now 2 test case giving wrong ans and rest giving TLE

can you solve this one please @aman212yadav

@anubhavb0011

initilaise ur cnt ,cnt1,cnt2 with 0
check ur updated code here->

why we have to intialize them with 0 as we are standing on head cnt should be 1?

consider these two list
1->2
1->2->3

diff will be 1 right ?
if u will initilaise ur cnt with 1
then while(cnt!=d) loop will never run and ur both the list will remain as it is.

that is why intialised cnt with 0

1 Like

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.