Why it is going in a infinite loop

Here is the code

Hello @hhiteshbansal_40d12e2460a1738e,

  1. Instead of comparing their data you should compare pointers,

if(l2==l1)

  1. each time you iterate over l2 you should make a second pointer pointing to l2

Like this,

while(l1->next!=NULL){
Node* temp = l2;
while(temp>next!=NULL){
if(temp==l1){
return temp;
}
temp = temp->next;
}
l1=l1->next;
}
return NULL;

why are we taking a second pointer temp .
can`t we take l2 instead of temp

also is there is any guarantee that node having same value will have same address in both linklist.

@hhiteshbansal_40d12e2460a1738e,

If two nodes don’t have the same address then our answer could probably be wrong,
for eg-

In this case your answer would be 1 but the correct answer is 3

@hhiteshbansal_40d12e2460a1738e,

If you don’t take temp then while iterating over second while loop, l2 would become null,
and for each node in l1, l2 would be null.

okay got it thanks :+1:

1 Like

@hhiteshbansal_40d12e2460a1738e,

Is your code working now?


can you tell me my mistake now

@hhiteshbansal_40d12e2460a1738e,

if(temp->next==l1->next){
return temp;
}

Change this,

if(temp==l1){
return temp;
}

@hhiteshbansal_40d12e2460a1738e,
Is your mistake clear now?

now it is showing :-
/bin/run.sh: line 4: 18 Killed ./exe

@hhiteshbansal_40d12e2460a1738e,

You are giving wrong input, try this:

1 2 3 -1
4 2 3 -1

IT IS STILL GIVING
runguard: warning: timelimit exceeded (wall time): aborting command
runguard: warning: command terminated with signal 15
DURING SUBMITING OF CODE

@hhiteshbansal_40d12e2460a1738e,

There is still one error, instead of writing like this,

while(l1->next!=NULL){
Node*temp=l2;
while(temp->next!=NULL){

It should be,

while(l1!=NULL){
Node*temp=l2;
while(temp!=NULL){

if,
l1 = 1 -> NULL
and l2 = 2 -> 3 -> 1 -> NULL
then o/p would simply be NULL in your case but the correct o/p is 1

Also, you haven’t submitted it even once, please try submitting your solution instead of compiling it.

@hhiteshbansal_40d12e2460a1738e,

Is it clear the last changes that I did?

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.