# LeetCode Odd Even Linked List

Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.

You should try to do it in place. The program should run in O(1) space complexity and O(nodes) time complexity.

Example:
Given `1->2->3->4->5->NULL`,
return `1->3->5->2->4->NULL`.

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 ...

```class Solution {
public:
ListNode *odd = new ListNode(0), *even = new ListNode(0);
ListNode *otail = odd, *etail = even;
while (fast1 || fast2) {
otail->next = fast1;
etail->next = fast2;
if (otail)otail = otail->next;
if (etail)etail = etail->next;
if (fast2)fast1 = fast2->next;
else fast1 = NULL;
if (fast1)fast2 = fast1->next;
else fast2 = NULL;
}
otail->next = even->next;
return odd->next;
}
};
```

```class Solution {
public:
ListNode *odd = new ListNode(0), *even = new ListNode(0);
int cnt = 1;
ListNode *cur = head, *otail = odd, *etail = even;
while(cur) {
if(cnt & 1) {
otail->next = cur;
otail = otail->next;
}
else {
etail->next  =cur;
etail = etail->next;
}
cur = cur->next;
++cnt;
}
otail->next = even->next;
etail->next = NULL; // caution!!!
return odd->next;
}
};
```