题目:
力扣
源代码:
struct ListNode* insertionSortList(struct ListNode* head){
//创建一个哨兵位
struct ListNode* pphead = (struct ListNode*)malloc(sizeof(struct ListNode));
pphead->next= head;
struct ListNode* s1 = pphead->next;
struct ListNode* s2 = s1->next;
//开始进入排序
while(s2)
{
if(s1->val<=s2->val)
{
s1=s1->next;
}
else
{
struct ListNode*prev = pphead;
while(prev->next->val<=s2->val)
{
prev = prev->next;
}
s1->next = s2->next;
s2->next = prev->next;
prev->next = s2;//在合适的位置插入
}
s2 = s1->next;
}
return pphead->next;
}