/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution { public ListNode insertionSortList(ListNode head) { if(head==null) return null; ListNode next=head; ListNode newhead=null; while(next!=null) { ListNode temp=next; next=next.next; temp.next=null; //开始插入 if(newhead==null) { newhead=temp; } else { ListNode i=newhead; ListNode tail=null; while(i!=null) { if(i.val>temp.val) { if(tail!=null) tail.next=temp; else newhead=temp; temp.next=i; break; } tail=i; i=i.next; } if(i==null) { tail.next=temp; } } } return newhead; } }