Please helpme Fix this error

Error:

Exception in thread “main” java.lang.NullPointerException
at Main.insertToList(Main.java:21)
at Main.main(Main.java:72)

Code Link

https://ide.geeksforgeeks.org/ptgeZG3InB

hello @shivampokhriyal

in java object are always passed by value, so in all function u need return ur updated node otherwise ur head will remain null.

image

here make return type Node and next should not be there
return ur updated head from this function.

image

here it should be
head=insertToList(head,val);

also once check ur alogorithm,
there u need handle cases like (no odd present , no even present)
one simple algorithm for this problem that preserve relative ordering is
a)prepare odd list
b) prepare even
c) then add head of even list to tail

Updated, but still getting the same error.

https://ide.geeksforgeeks.org/snqaacqkdt

please see.

@shivampokhriyal

check now->

the code is not giving segfault.
but u need to change ur algorithm it is giving tle.
follow this->

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.