/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* insertionSortList(ListNode* head) {
if(!head||!head->next)
return head;
ListNode *curr,*pre;
curr=head->next;
pre=head;
while(curr)
{
if(curr->val<head->val)
{
pre->next=curr->next;
curr->next=head;
head=curr;
curr=pre->next;
}
else
{
ListNode *p=head;
while(p!=pre)
{
if(curr->val<p->next->val)
{
pre->next=curr->next;
curr->next=p->next;
p->next=curr;
curr=pre->next;
break;
}
p=p->next;
}
if(p==pre)
{
curr=curr->next;
pre=pre->next;
}
}
}
return head;
}
};
leetcode 147: Insertion Sort List
最新推荐文章于 2022-10-08 18:56:27 发布