用插入排序对链表排序
样例
样例 1:
输入: 0->null
输出: 0->null
样例 2:
输入: 1->3->2->0->null
输出 :0->1->2->3->null
/**
* Definition of singly-linked-list:
* class ListNode {
* public:
* int val;
* ListNode *next;
* ListNode(int val) {
* this->val = val;
* this->next = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param head: The first node of linked list.
* @return: The head of linked list.
*/
ListNode * insertionSortList(ListNode * head) {
// write your code here
ListNode*newhead=new ListNode(0);
while(head)
{
ListNode*list=newhead;
while(list->next&&list->next->val<head->val)
list=list->next;
ListNode*tmp=head->next;
head->next=list->next;
list->next=head;
head=tmp;
}
return newhead->next;
}
};