您的位置:首页 > 其它

Insertion Sort List

2014-02-24 04:53 295 查看
public class Solution {
public ListNode insertionSortList(ListNode head) {
if(head==null) return head;
ListNode safe = new ListNode(Integer.MIN_VALUE);
ListNode p2 = head;
while(p2!=null){
ListNode pre = find(safe,p2);
ListNode next = p2.next;
p2.next = pre.next;
pre.next = p2;
p2 = next;
}
return safe.next;
}
public ListNode find(ListNode p1,ListNode p2){
ListNode cur = p1, pre = null;
while(cur!=null && cur.val<=p2.val){
pre = cur;
cur = cur.next;
}
return pre;
}
}


View Code
Sort a linked list using insertion sort
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: