When i take custom input for two linked lists, they always have two separate addresses, so the loop will never terminate. How do i get rid of this issue?
What is wrong with my code?
Hi @tusharnitharwal,
since u r taking inputs of 2 lists separately, all d elements of both the linked list will have different addresses. So, it is always preferable to compare data in such cases. If u want to assign same address to same data, u can map data to address and use that address in ur linked list.
eg. if list1={1,2,3,4}. map all address. if list2={3,5,6} when 3 comes check map and start from that address of 3.
Hope dis resolves ur doubt.
if not clear, pls post ur doubt.
But this would cause a problem if the values of the list become equal before the meeting point
https://ide.codingblocks.com/s/199560 One test case is not working and it probably has the same issue
@tusharnitharwal, try to do head=head->next after comparing data of 2 lists.
in ur present code, if input is like 1 5 1 5, it will give -1 as ouput but it should be 5.
Hope dis helps.
in d present senior, comparing nodes will not work. Since present inputs doesnt specify which nodes have to be common but only tells which data is common, comparing address is not good.
But when u try doing questions on site where they accept only function of intersection, u can try comparing nodes since the given inputs are taken already by the problem setter.
Also in above senario, see if question demands comparing data or nodes.
Hope dis helps and resolves ur doubt.
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.