单链表的插入排序
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode insertionSortList(ListNode head) {
if (head != null && head.next != null) {
ListNode otherHead = null;
otherHead = head.next;
head.next = null;
while (otherHead != null) {
ListNode node = otherHead;
otherHead = otherHead.next;
node.next = null;
ListNode t = head;
ListNode pre = head;
while(t != null){
if(t.val > node.val){
break;
}
pre = t;
t = t.next;
}
if(t == pre){
node.next = t;
head = node;
}
else{
pre.next = node;
node.next = t;
}
}
}
return head;
}
}