/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
void reorderList(ListNode* head) {
vector<ListNode*>store;
ListNode*t=head;
while(t)
{
store.push_back(t);
t=t->next;
}
vector<ListNode*>ans;
int l=0,r=store.size()-1;
while(1)
{
ans.push_back(store[l++]);
if(l>r)
break;
ans.push_back(store[r--]);
if(l>r)
break;
}
for(int i=0;i<ans.size();i++)
{
ans[i]->next=nullptr;
}
for(int i=0;i<ans.size()-1;i++)
{
ans[i]->next=ans[i+1];
}
}
};
LeetCode143
最新推荐文章于 2024-08-23 01:05:03 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)