题目
快慢指针找到中间节点后,后一半反转 然后交叉合并。。
class Solution {
public:
ListNode* getmid(ListNode* x){
ListNode *slow=x,*fast=x;
while(fast&&fast->next) fast=fast->next->next,slow=slow->next;
return slow;
}
ListNode* reverse(ListNode* x){
if(!x||!(x->next)) return x;
ListNode *nex=x->next,*ans;
x->next=NULL,ans=reverse(nex);
nex->next=x;
return ans;
}
ListNode* merge(ListNode* x,ListNode* y){
ListNode *now=new ListNode();
while(x&&y){
now=now->next=x,x=x->next;
now=now->next=y,y=y->next;
}
if(x) now->next=x;
if(y) now->next=y;
return now->next;
}
void reorderList(ListNode* x) {
if(!x) return;
ListNode *mid=getmid(x),*nex=mid->next;
mid->next=NULL;
x=merge(x,reverse(nex));
}
};