



class Solution {
public:
void reorderList(ListNode* head)
{
ListNode* cur1 = head,*cur2 = head;
//找中间
while(cur2 && cur2->next)
{
cur1 = cur1->next;
cur2 = cur2->next->next;
}
//逆序后面(头插三指针)
ListNode* newhead2 = new ListNode();
newhead2->next = cur1;
ListNode* cnext = cur1->next;
cur1->next = nullptr;
while(cnext)
{
ListNode* nnext = cnext->next;
newhead2->next = cnext;
cnext->next = cur1;
cur1 = cnext;
cnext = nnext;
}
//合并
ListNode* newhead1 = new ListNode();
ListNode* tail = newhead1;
while(cur1)
{
tail->next = head;
tail = head;
if(head->next == nullptr) break;
head = head->next;
tail->next = cur1;
tail = cur1;
cur1=cur1->next;
}
//释放哨兵位头节点
delete newhead1;
delete newhead2;
}
};