Sort a linked list using insertion sort.
原题链接:https://oj.leetcode.com/problems/insertion-sort-list/
题目:用插入排序对一个链表排序。
public ListNode insertionSortList(ListNode head) {
ListNode newHead = new ListNode(-1);
ListNode pre = newHead;
ListNode current = head;
while(current != null){
pre = newHead;
while(pre.next != null && pre.next.val < current.val)
pre = pre.next;
ListNode next = current.next;
current.next = pre.next;
pre.next = current;
current = next;
}
return newHead.next;
}