https://leetcode.com/problems/insertion-sort-list/
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode insertionSortList(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode h = new ListNode(0), p = head, pre = h, q = head.next;
h.next = head;
p.next = null;
while (q != null) {
while (p != null && q.val >= p.val) {
pre = p;
p = p.next;
}
ListNode t = q.next;
pre.next = q;
q.next = p;
q = t;
pre = h;
p = h.next;
}
return h.next;
}
}
728

被折叠的 条评论
为什么被折叠?



