/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类
* @return ListNode类
*/
struct ListNode* oddEvenList(struct ListNode* head ) {
// write code here
struct ListNode* temp=NULL;
struct ListNode* last=temp;
struct ListNode* p=head;
struct ListNode* q=NULL;
int i=1;
if(head==NULL || head->next==NULL || head->next->next==NULL) //特殊情况另作考虑
return head;
while(p)
{
struct ListNode* pre=NULL; //需要分离的结点
q=p;
p=p->next;
if(p==NULL) //确保此处不为空,不然下面的程序段会段错误
break;
i++;
if(i%2==0)
{
pre=p;
p=p->next;
pre->next=NULL;
i++;
q->next=p;
}
if(temp==NULL)
{
temp=pre;
last=pre;
}
else
{
last->next=pre;
last=pre;
}
}
q->next=temp;
return head;
}