NC2重排链表
//测试用例
//1234
//1423
//-
//12345
//15243
//-
//12345
//123/45
//123/54
//15243
//解题思路
//1.快慢指针找到链表的中间节点
//2.从中间节点将链表断开
//3.翻转后半段链表
//4.合并链表
class Solution {
public:
//反转链表
ListNode*reverselist(ListNode*head){
if(head==nullptr||head->next==nullptr){
return head;
}
ListNode*cur=head;
ListNode*newhead=nullptr;
while(cur){
ListNode*next=cur->next;
cur->next=newhead;
newhead=cur;
cur=next;
}
return newhead;
}
void reorderList(ListNode *head) {
if(head==nullptr||head->next==nullptr){
return;
}
//1.快慢指针找到链表的中间节点
ListNode*slow=head;
ListNode*fast=head;
while(fast&&fast->next){
slow=slow->next;
fast=fast->next->next;
}
//2.从中间节点将链表断开
ListNode*tmp=slow->next;
slow->next=nullptr;//断开
//3.翻转后半段链表
ListNode*newhead=reverselist(tmp);
//3.合并链表
//head-123
//newhead-54
//15243
ListNode*cur=head;
int k=0;
//循环控制条件&&-一个走完就结束
while(cur&&newhead){
if(k%2==0){
ListNode*next=cur->next;
cur->next=newhead;//链接
cur=next;
}else{
ListNode*next=newhead->next;
newhead->next=cur;//链接
newhead=next;
}
k++;
}
return;
}
};