import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* }
*/
public class Solution {
/**
*
* @param head ListNode类
* @return ListNode类
*/
public ListNode insertionSortList (ListNode head) {
// write code here
if(head==null||head.next==null) return head;
ListNode pre = head;//pre指向已经有序的节点
ListNode cur = head.next;//cur指向待排序的节点
ListNode aux = new ListNode(-1);//辅助节点
aux.next = head;
while(cur!=null){
if(cur.val<pre.val){
//先把cur节点从当前链表中删除,然后再把cur节点插入到合适位置
pre.next = cur.next;
//从前往后找到l2.val>cur.val,然后把cur节点插入到l1和l2之间
ListNode l1 = aux;
ListNode l2 = aux.next;
while(cur.val>l2.val){
l1 = l2;
l2 = l2.next;
}
//把cur节点插入到l1和l2之间
l1.next = cur;
cur.next = l2;//插入合适位置
cur = pre.next;//指向下一个待处理节点
}else{
pre = cur;
cur = cur.next;
}
}
return aux.next;
}
}