/** * struct ListNode { * int val; * struct ListNode *next; * ListNode(int x) : val(x), next(nullptr) {} * }; */ class Solution { public: ListNode* oddEvenList(ListNode* head) { int i=0; int count=1; vector <int> a; vector <int> b; ListNode*p=head; while(p) { if(count%2==0) { b.push_back(p->val); } else { a.push_back(p->val); } count++; p=p->next; } ListNode*ans=NULL; ListNode*t=NULL; int len1=a.size(); int len2=b.size(); if(len1==0 && len2==0) { return NULL; } else { if(len1==0) { ans=t=new ListNode(b[0]); for(i=1;i<len2;i++) { t->next=new ListNode(b[i]); t=t->next; } } else { ans=t=new ListNode(a[0]); for(i=1;i<len1;i++) { t->next=new ListNode(a[i]); t=t->next; } for(i=0;i<len2;i++) { t->next=new ListNode(b[i]); t=t->next; } } } return ans; } };