解题思路:画图分析
/**
* 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* dummy =new ListNode(-1);
dummy->next=head;
ListNode* pre=dummy,*cur =head,*next=head->next,*h2=next;
while(next!=NULL&&next->next!=NULL)
{
cur->next=next->next;
cur=next->next;
next->next=cur->next;
next=next->next;
}
cur->next=h2;
return dummy->next;
}
};