Sort a linked list using insertion sort.
A graphical example of insertion sort. The partial sorted list (black) initially contains only the first element in the list.
With each iteration one element (red) is removed from the input data and inserted in-place into the sorted list
Algorithm of Insertion Sort:
Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list.
At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there.
It repeats until no input elements remain.
题目大意 给已知链表做插入排序
class Solution {
public:
ListNode* insertionSortList(ListNode* head) {
ListNode *dum= new ListNode(0);
while(head)
{
ListNode *p=dum;
ListNode *q=head->next;
while(p->next&&p->next->val<head->val)
p=p->next;
head->next=p->next;
p->next=head;
head = q;
}
return dum->next;
}
};