class Solution {
public:
ListNode* insertionSortList(ListNode* head) {
if(!head)return head;
queue<ListNode*>q;
ListNode* p=head;
while(p){
q.push(p);
p=p->next;
}
head=NULL;
while(!q.empty()){
p=head;
ListNode *past_p=NULL;
ListNode* t=q.front();
q.pop();
while(p&&p->val<t->val){
past_p=p;
p=p->next;
}
t->next=p;
if(past_p==NULL)
head=t;
else past_p->next=t;
}
return head;
}
};
147. Insertion Sort List
最新推荐文章于 2024-06-02 19:59:13 发布