public class Solution {
public ListNode oddEvenList(ListNode head) {
if (head != null) {
ListNode odd = head, even = head.next, evenHead = even;
while (even != null && even.next != null) {
odd.next = odd.next.next;
even.next = even.next.next;
odd = odd.next;
even = even.next;
}
odd.next = evenHead;
}
return head;
}}
Simple O(N) time, O(1), space Java solution.


@xuyirui
I was the first person who got this elegant solution
Maybe. But you weren't the first who posted it. That was yangneu2015.

This solution also assumes that the values inside of the nodes are going to be the same as the number of the order in which they appear. We're not ordering them by the intrinsic property of whether their order number in the list is odd or even, but rather by the oddness or evenness of the numbers they contain
The problem would allow a list like
1,6,9,4,5,7,3. This solution would not work there.
