用插入排序对链表进行排序。
详见:https://leetcode.com/problems/insertion-sort-list/description/
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* insertionSortList(ListNode* head) {
ListNode* helper=new ListNode(-1);
ListNode* cur=helper;
while(head)
{
ListNode* next=head->next;
cur=helper;
while(cur->next&&cur->next->val<=head->val)
{
cur=cur->next;
}
head->next=cur->next;
cur->next=head;
head=next;
}
return helper->next;
}
};