1、
Sort a linked list using insertion sort.
Given 1->3->2->0->null
, return 0->1->2->3->null
.
2、
/** * Definition for ListNode. * public class ListNode { * int val; * ListNode next; * ListNode(int val) { * this.val = val; * this.next = null; * } * } */ public static ListNode insertionSortList(ListNode head) { // 1创建一个新的,用来接收 ListNode dommy = new ListNode(0); while (head != null) { //地址赋予node,node即dommy ListNode node = dommy; //每次逐条进行判断,大的往前面挪,重新排序 while (node.next != null && node.next.val < head.val) { node = node.next; } ListNode temp = head.next; head.next = node.next; node.next = head; head = temp; } return dommy.next; }