* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) : val(x), next(nullptr) {}
* };
*/
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类
* @return ListNode类
*/
ListNode* oddEvenList(ListNode* head) {
// write code here
if(!head || !head->next) return head;
ListNode* odd = head, *even = head->next;
ListNode* tmp = even;
while(odd->next && odd->next->next){
//odd->next是判断偶节点是否还存在
//odd->next->next是判断下一个奇数节点是否还存在
odd->next = odd->next->next;
odd = odd->next;
even->next = even->next->next; //even->next->next 最后一定是指向空的
even = even->next;
}
odd->next = tmp;
return head;
}
};