题意:给出一个未排序好的单链表,用插入排序来实现排序
代码如下:
class Solution{public ListNode insertionSortList(ListNode head){if (null == head || null == head.next) return head;ListNode dummy = new ListNode(0), outer = head;dummy.next = head;while (outer != null && outer.next != null){ListNode inner = dummy;while (inner != outer){if (inner.next.val > outer.next.val){ListNode tmp = inner.next;inner.next = outer.next;outer.next = outer.next.next;inner.next.next = tmp;break;}inner = inner.next;}if (inner == outer) outer = outer.next;}return dummy.next;}}