Sort a linked list using insertion sort.
单链表插入排序,没啥好说的
class Solution {
public:
ListNode * pickMin(ListNode * & head){
ListNode * mins = head, *cur = head;
while(cur){
if(cur->val < mins->val) mins = cur;
cur = cur ->next;
}
swap(head->val,mins->val);
mins = head;
head = head->next;
return mins;
}
ListNode *insertionSortList(ListNode *head) {
ListNode pre(-1);
ListNode * cur = ⪯
while(head){
cur->next = pickMin(head);;
cur = cur->next;
}
return pre.next;
}
};