题解 | #链表的奇偶重排#
二叉树中的最大路径和
http://www.nowcoder.com/practice/da785ea0f64b442488c125b441a4ba4a
/**
* 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
ListNode* odd = new ListNode(-1), *even = new ListNode(-1);
ListNode* ot = odd, *et = even;
int num = 0;
while(head){
num++;
if(num&1){
ot->next = head;
ot = head, head = head->next;
ot->next = NULL;
}else{
et->next = head;
et = head, head = head->next;
et->next = NULL;
}
}
if(odd->next)
ot->next = even->next;
return odd->next;
}
};