No output of code

I am getting no output. On CB IDE, I am getting TLE.

In my opinion of the code,
Space Complexity = O(N)
and Time Complexity = O(2N).
Is it Correct?

I did this in this way as there were no constraints…

Is there any better approach?

the link of my code :

@sanjit_15
your code is almost correct but you forget to odd=odd->next; after line 36 and head=head->next after 43. That’s why your code get stuck in infine loop and it’s giving TLE.
Also in 40 i.e. if((head->data)&1==0), use proper braces otherwise it’ll given incorrect ans. Change it to if(((head->data)&1)==0). Your code will work for all testcases now.

Thank you. I got correct answer after the following changes. I just want to know if we can do the question without using another Linked List, in O(1) space…

Yes you can do this without using another array. You need two pointer one points to beginning of even and other points of end of odd number. whenever you get odd number start swapping element from beginning of even pointer and move the odd element to end of odd pointer.You can check it;s code here

1 Like