/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* oddEvenList(ListNode* head) {
if(head==NULL || head->next==NULL){
return head;
}
ListNode * oddRunner=head;
ListNode* evenHead=head->next;
ListNode* evenRunner=evenHead;
while(oddRunner->next!=NULL && evenRunner->next!=NULL){
oddRunner->next=oddRunner->next->next; // set next as (jump two steps)
evenRunner->next=evenRunner->next->next;
oddRunner=oddRunner->next; // jump
evenRunner=evenRunner->next;
}
oddRunner->next=evenHead;
return head;
// Method A: Using iterative two pointer
// if(head==NULL || head->next==NULL){
// return head;
// }
// ListNode* Odd=head;
// ListNode* Evenhead=head->next;
// ListNode* Even=Evenhead;
// // starting from 3rd node
// int i=3; // 1 indexed
// ListNode* curr=NULL;
// curr=Evenhead->next; // evenhead NULL is handeled in base case
// while(curr!=NULL){
// if(i&1){ // odd
// Odd->next=curr;
// Odd=Odd->next;
// }else{
// Even->next=curr;
// Even=Even->next;
// }
// curr=curr->next;
// i++;
// }
// if(Even!=NULL){
// Even->next=NULL; // mark end at even of ll
// }
// Odd->next=Evenhead;
// return head;
}
};