Insertion Sort List
Sort a linked list using insertion sort.
ListNode *insertionSortList(ListNode *head) {//添加一个最小头结点,然后添加一个定位置的函数
ListNode dummy(INT_MIN);
for(ListNode* cur =head;cur !=nullptr;)
{
auto pos =findInsertPos(&dummy,cur->val);
ListNode* tmp =cur->next;
cur->next =pos->next;
pos->next =cur;
cur =tmp;
}
return dummy.next;
}
ListNode* findInsertPos(ListNode* head,int x)
{
ListNode* pre=nullptr;
for(ListNode* cur =head;cur!=nullptr&&cur->val <=x;pre =cur,cur =cur->next)
;
return pre;
}