/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
void reorderList(ListNode* head) {
if(!head)return;
stack<ListNode*>s;
ListNode *slow=head;
ListNode *fast=head;
while(fast!=NULL&&fast->next!=NULL){
slow=slow->next;
fast=fast->next->next;
}
if(fast)slow=slow->next;
while(slow){
s.push(slow);
slow=slow->next;
}
ListNode*p=head;
ListNode*next_p=p->next;
while(!s.empty()){
ListNode *t=s.top();
s.pop();
t->next=next_p;
p->next=t;
p=next_p;
next_p=p->next;
}
p->next=NULL;
}
};
143. Reorder List
最新推荐文章于 2022-03-07 13:49:13 发布