https://leetcode.com/problems/insertion-sort-list/
Sort a linked list using insertion sort
public ListNode InsertionSortList(ListNode head)
{
if (head == null || head.next == null)
return head;
ListNode pre = new ListNode(0),next,tmp;
pre.next = head;
while (head.next != null)
{
next = head.next;
if (head.val > next.val)
{
tmp = pre;
while (pre.next.val < next.val)
pre = pre.next;
head.next = next.next;
next.next= pre.next;
pre.next = next;
pre = tmp;
}
else
{
head = next;
}
}
return pre.next;
}