Why it is giving wrong output?
Hi @raj.verma5454,
Try to modify ur code. In ur current code, in ur “dia ans=countodd(head);” ans.od stores the last odd number and ans.en stores the last even number. So, when u again try to build linked list using ans, linked list only contains ans.od and ans.en.
eg. n=4 and list={1,2,3,4}
so in d end, ans.en=4 and ans.od=3., So, when u make the final list, output={3,3,4,4}.
So, instead of declaring ans.en and ans.od as int, declare it as node* so that u can make a linked list out of it.
eg. n=4 and list={1,2,3,4}
so ans.od will be {1,3} and ans.en={2,4}. So, try to make final output using these lists.
Hope dis resolves ur doubt.
If u still have any doubts, feel free to post them here.
I got the result , but is there any efficient approach ??
This is O(n) approach but is not memory efficient since we need to make 2 more lists(odd and even).
In order to increase the efficiency more, u can put odd nodes at end of list. Like if input={1,2,3,4}. run for loop on array, if u encounter odd node, place it at end of the list.
in above example ,list will look like
{1,2,3,4}->{2,3,4,1}->{2,4,1,3}.
Hope dis 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.