Problem in linked list

node*temp=head->next;

while(((temp->next->data)&1==0)||(temp->next==NULL))

{

if(temp->next==NULL)

{ return; }

temp=temp->next;

}

in this block of my code , suppose my input is 1 2 2 4 1 , my head pointer is pointing to 1 and i want my temp pointer to point at a node which have a odd number in its next node i.e 4 here ( next number is 1 ) so i wrote this while loop to do it but somehow its not going in the loop , i need to know why my while loop is not working

Hey @rohansrivastav please share your code using ide.codingblocks.com by saving it and sharing it’s url so that i can help you.

Have checked your logic bro, and yes it’s a fault in the while loop which you have mentioned
Try solving this question in this way, cause while debugging your code for test case
10
1 2 3 5 7 2 4 6 3 8
your while loop was only working for 1 and not for rest. Rest i would suggest you to dry run once. You will get the error.

i have tried dry running it multiple times , still its not clear and moreover when you use %2(instead of &1 )
to check whether the data is odd or even , it is giving me a segmentation fault, so just by changing &1 to %2 , it is giving me a segmentation fault. if possible please go through it

see this: here what is actually happening in test case:

10
1 0 3 5 7 2 4 6 3 8
  ^
  |

when your temp comes to digit 0, it gets at the tail of the linked list, whereas we have to make it as head of even linked list. That’s the only error you are facing.


It’s logical error of your code, that’s why i can’t debug it. Take reference from this code, and then code as per this logic. You will get it.

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.