Even After Odd Linked List

Question Link : https://hack.codingblocks.com/contests/c/511/315

My Solution Link : https://ide.codingblocks.com/s/37959

Please see the code and tell me why the order of even and odd nos are opposite coming

If possible, Please correct the code and comment about it.

Have you copied your code from some online website… It seems you have just copied and pasted the code without even checking what you are copying as you have copy and pasted the c syntax exactly without making any changes for converting it to c++. Are you still getting some points on hackerblocks??

Your code gives wrong input for various cases.
eg
For input
5
1 4 3 2 1
It gives the output
1 3 1 2 4

But the correct output would be
1 3 1 4 2

As you just have to display the even numbers after the odd ones, but their relative order should remain the same.

please see the code once as i have updated it and correct where i am wrong.

No
I haven’t copied,…

Share the link of your updated code also.

https://ide.codingblocks.com/s/37959

This is the same earlier code only. You are supposed to save the code again after editing to reflect the changes.

Please save it again after making the changes. Then you’ll get new link, and share that here.

I have changed it but i can’t understand why it is coming odd after even but we have to get even after odd…

can you please post the link of your updated code… so that we can help you with that in realising exactly where your code is going wrong.

It is updated code… Please do the change so that result is even after odd

https://ide.codingblocks.com/s/37959

Hey for that case you just need to change
(!=) to (==) and vice versa in all your conditions. :joy:

https://ide.codingblocks.com/s/39949

I have commented and made changes. Have a look at it.

Done.
Thanks…:grinning::grinning::grinning::grinning:

//List stl
int main()
{
#ifndef ONLINE_JUDGE
freopen(“input.txt”, “r”, stdin);
freopen(“output.txt”, “w”, stdout);
#endif
int n;
cin>>n;
listl(n);
for(auto i=l.begin();i!=l.end();i++){
cin>>(*i);
}
for(auto i=l.begin();i!=l.end();i++){
if((*i)%2!=0){
cout<<(*i)<<" “;
}
}
for(auto i=l.begin();i!=l.end();i++){
if((*i)%2==0){
cout<<(*i)<<” ";
}
}
}