You should try to do it in place. The program should run in O(1) space complexity and O(nodes) time complexity.
Note:
The relative order inside both the even and odd groups should remain as it was in the input.
The first node is considered odd, the second node even and so on …
解法1:
考虑1->2->3->4这个情况,实际上只需要修改成1->3->4,2->4就可以了,然后把odd指针往下移动一格,even指针往下移动一格就可以了。
C++
Java
另一种写法