do we need to maintain any sorting order for odd or in even sub sequence like 121234521 should be written as 113512242 or it can be written as 131154222 something
Even after odd problem
I think that both will be right for this question. But for your own practice try to maintain the order given in the original linked list.
Hope it Helps.
i was thinking of doing it like traverse through the entire link list and whenever any odd no is encountered then shift that node at the beginning of the list, will that work
That would shuffle up the order i guess. You can try that way. Alternatively you can maintain a pointer pointing to the first even number occurrence and then replace with the first odd number found after that and then increase this pointer to the next node.
Hope it Helps.
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.
@Learning_bunny
Your logic is completely incorrect
See this for reference https://www.geeksforgeeks.org/segregate-even-and-odd-elements-in-a-linked-list/
can you tell me how my logic is incorrect i am doing it like start traversing the list from the beginning and if any odd no is encountered then shift that node to the beginning to the list
@Learning_bunny
While what you’re saying sounds fine
Your code is actually not able to do that
And despite the logic begin correct factually, its time complexity is 0(n^2) we need 0(n) here
Instead of traversing list every time you should maintain a tail pointer to do insertion in 0(1)
Finally you set both ptr1 and ptr2 to head initially which is incorrect
i am not trying to do that, actually what i am trying to do is take 3 pointers p p1 and p2 . now p1 and p2 points to head and p points to next of head in the first iteration whenever an odd is encountered at pointer p then pointer p1 will point to next of p now p2 will point to p and the ointer p(which is odd) will point to head and head will point to pointer p. In way whole list is maintained
and the thing u said for traversing the list again and again for that i was thinking according to my approach p1 is the pointer which points to next node to be evaluated for odd or even, if can store that somehow then its complexity will be reduced as we make p p1 p2 point on the same node and odd or even is checked and then same function as previous is used
please think about it and let me know if i were wrong thankyou
@Learning_bunny
I’ll be frank with you buddy. I could not understand your approach via the code neither the explanation you just gave. I’m sorry for it but I couldn’t. But I would suggest you to open the GFG link I sent since the approach there is so much easier to work with and explain.
If you still feel like you need help with your approach only I request you to close this doubt and you can then reopen it and someone else will be assigned to it.
Hope that helps both of us.
Happy Coding !
that file is visible to me now can u please resend it
the gfg link you mentioned in previous comment