1 test case failing

swapping the data will not work correctly
because due to this order will change

for input
4
1 2 4 3
Your Output
1 3 4 2
Correct Output
1 3 2 4

So I think I should make two separate lists for odd and even data and at last just connect them.
Please suggest any other approach if it’s better than mine or else I should go with this separate list approach?

yes , you can also do better

make a partition index
before which all nodes have odd values and after partition index all nodes have even value

traverse in the list and try to update the partition index

Reference Code

that’s awesome. Thank You!

But i tried using odd/even separate LL approach and having error please fix.

Your Mistake
your add to linked list function is incorrect
i corrected it check

Logic :

  1. go to end of list
  2. now add the node of given data at end

Modified Code

thanks a lot. you can close this thread.