单纯的链表插入排序
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 now = head.next; head.next = null; while (now != null) { ListNode injectNode = now; now = now.next; injectNode.next = null; head = insert(head, injectNode); } return head; } private ListNode insert(ListNode head, ListNode injectNode) { if (injectNode.val < head.val) { injectNode.next = head; return injectNode; } ListNode preNode = head; ListNode node = head.next; while (node != null) { if (injectNode.val < node.val) { injectNode.next = node; preNode.next = injectNode; return head; } preNode = node; node = node.next; } preNode.next = injectNode; return head; } }