Sort a linked list using insertion sort.
含义:用插入排序来对列表进行排序
1 public ListNode insertionSortList(ListNode head) { 2 if (head==null || head.next==null ) return head; 3 ListNode helper = new ListNode(0); 4 ListNode current = head; 5 while (current!=null) 6 { 7 ListNode prevInHelper = helper; 8 while (prevInHelper.next!=null && prevInHelper.next.val<current.val) 9 { 10 prevInHelper = prevInHelper.next; 11 } 12 ListNode temp = current.next; 13 current.next = prevInHelper.next; 14 prevInHelper.next = current; 15 current = temp; 16 } 17 return helper.next; 18 }