- ListNode *insertionSortList(ListNode *head) {
- if(head==NULL || head->next==NULL) return head;
- ListNode *end=head, *endf=head, *forin=end->next, *temp=NULL;
- while(forin!=NULL)
- {
- if(forin->val<head->val)
- {
- end->next=forin->next;
- forin->next=head;
- head=forin;
- forin=end->next;
- }
- else if(forin->val>=end->val)
- {
- end=forin;
- forin=end->next;
- }
- else
- {
- for(temp=head; temp!=end; temp=temp->next)
- {
- if(temp->val==forin->val || (temp->val<forin->val && temp->next->val>forin->val))
- {
- end->next=forin->next;
- forin->next=temp->next;
- temp->next=forin;
- break;
- }
- }
- forin=end->next;
- }
- }
- return head;
- }
- //该片段来自于http://www.codesnippet.cn/detail/2005201512624.html
来源: http://www.codesnippet.cn/detail/2005201512624.html