/**
* 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) return head;
ListNode p = head;
ListNode q = head.next;
while (q!=null) {
if (q.val>=p.val) {
p = q;
q = q.next;
} else {
ListNode h = head;
if (q.val<=h.val) {
ListNode qnext = q.next;
q.next = head;
head = q;
q = qnext;
p.next = q;
} else {
while (q.val>h.val && q.val>=h.next.val) h = h.next;
if (q.val>=h.val && q.val <= h.next.val) {
ListNode qnext = q.next;
q.next = h.next;
h.next = q;
q = qnext;
p.next = q;
}
}
}
}
return head;
}
}
没有评论:
发表评论