Even-after-Odd problem

Do I need to take the whole list first and then rearrange them or I can also arrange them accordingly while taking in the input???

No take the whole list first as it is. And then rearrange.

https://ide.codingblocks.com/s/191928 Pls check tis code, all testcases are not being passed by it

There is no need of a tail pointer. Just traverse the linked list from the start and if you find an even number, push it to the end of the linked list. That’s it.
And to avoid any repetitions, you can use a map.

Also dry run your code for the following linked lists -
1 1 1 2
1 2 2 1
You will get to know about your mistake.

Thanks a lot for the help, can you please tell me what a map is, or give me some reference or link to study it from a source which explains it properly


http://www.cplusplus.com/reference/map/map/

You can study from these two sources. These will be sufficient.

@Tushar-Goel-1587999611286693 Also please mark your doubt as resolved now.

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.

Thanks a lot for the help and to suggest the sorce

1 Like

@Tushar-Goel-1587999611286693 what is the issue?

The code isn’t passing all the test cases, though it appears to be right

Can you pls tell what I am not considering

@Tushar-Goel-1587999611286693 What you are doing will change the relative ordering of the numbers
Like 2 3 5 4 Will become 5 3 2 4 Whereas the correct output would be 3 5 2 4


Here have a look at this code. If this resolves your doubt mark it as resolved.

Thanks a lot to tell about the mistake

1 Like