LeetCode刷题记120
147. 对链表进行插入排序
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode insertionSortList(ListNode head) {
if (head == null) return null;
ListNode pre = head;
ListNode cur = head.next;
while (cur != null) {
if (pre.val <= cur.val) {
pre = cur;
cur = cur.next;
} else {
if (head.val > cur.val) {
pre.next = cur.next;
cur.next = head;
head = cur;
} else {
ListNode tmp1 = head;
ListNode tmp2 = head.next;
while (tmp2 != null) {
if (tmp2.val <= cur.val) {
tmp1 = tmp2;
tmp2 = tmp2.next;
} else {
pre.next = cur.next;
cur.next = tmp2;
tmp1.next = cur;
break;
}
}
}
cur = pre.next;
}
}
return head;
}
}
3/3
120/150