将链表节点顺序重组:奇数节点在前,偶数节点在后;
class Solution {
public:
ListNode* oddEvenList(ListNode* head) {
if(head==NULL)
return head;
ListNode* end_odd=head;
ListNode* cur_even=head->next;
while(end_odd&&cur_even)
{
ListNode *next_odd=cur_even->next;
if(next_odd)
{
cur_even->next=next_odd->next;
//end_odd->next=next_odd;
//next_odd->next=cur_even;
next_odd->next=end_odd->next;
end_odd->next=next_odd;
end_odd=end_odd->next;
cur_even=cur_even->next;
}
else
break;
}
return head;
}
};