/**
* 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 || head->next->next == NULL) return head;
ListNode* oddDummy = new ListNode(0); //为了方便写while循环,这里设置两个头结点
ListNode* evenDummy = new ListNode(0);
ListNode* oddList = oddDummy;
ListNode* evenList = evenDummy;
int i = 1;
while(head != NULL){
if(i % 2 == 1){
oddList->next = head;
oddList = oddList->next;
}else{
evenList->next = head;
evenList = evenList->next;
}
i++;
ListNode* tmp = head->next;
head->next = NULL;
head = tmp;
}
oddList->next = evenDummy->next;
return oddDummy->next;
}
};