ListNode* oddEvenList(ListNode* head)
{
if(!head) return head;
ListNode *odd=head, *evenhead=head>next, *even = evenhead;
while(even && even>next)
{
odd>next = odd>next>next;
even>next = even>next>next;
odd = odd>next;
even = even>next;
}
odd>next = evenhead;
return head;
}
Simple C++ solution, O(n) time, O(1) space



Maybe better solution (Deduced 4 times '>' operator call within the loop) :
ListNode* oddEvenList(ListNode* head) { if (!head) return head; auto odd = head, evenHead = head>next, even = evenHead; while (even && even>next) { odd = odd>next = even>next; even = even>next = odd>next; } odd>next = evenHead; return head; }

@csk If we switch the first two lines of the while loop,like even>next=even>next>next comes first then I am getting a null pointer error,could you highlight why?

@wfxr I can't understand your solution about this problem,for instance,
odd=odd>next=even>next? why do it ?

@wfxr said in Simple C++ solution, O(n) time, O(1) space:
Maybe better solution (Deduced 4 times '>' operator call within the loop) :
ListNode* oddEvenList(ListNode* head) { if (!head) return head; auto odd = head, evenHead = head>next, even = evenHead; while (even && even>next) { odd = odd>next = even>next;??? even = even>next = odd>next; } odd>next = evenHead; return head; }