/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode insertionSortList(ListNode head) {
if (head == null || head.next == null) return head ;
ListNode dump = new ListNode(0);
dump.next = head ;
ListNode cur = head.next;
head.next = null;
ListNode last = head ;
while (cur != null){
if (cur.val >= last.val){ //放到last后面即可
last.next = cur ;
cur = cur.next ;
last = last.next ;
last.next = null ;
}else{
ListNode first = dump ;
while (first.next != null && first.next.val < cur.val){
first = first.next;
}
//将cur插入到first和first.next之间
ListNode temp = cur;
cur = cur.next ;
temp.next = first.next ;
first.next = temp;
}
}
return dump.next;
}
}
05-15
869
12-05
1万+
09-28
891
04-21
1259
08-03
4248
08-04
172