首页 > 其他 > 详细

Insertion Sort List

时间:2014-02-25 03:04:05      阅读:341      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
 1 public class Solution {
 2     public ListNode insertionSortList(ListNode head) {
 3         if(head==null) return head;
 4         ListNode safe = new ListNode(Integer.MIN_VALUE);
 5         ListNode p2 = head;
 6         while(p2!=null){
 7             ListNode pre = find(safe,p2);
 8             ListNode next = p2.next;
 9             p2.next = pre.next;
10             pre.next = p2;
11             p2 = next;
12         }
13         return safe.next;
14     }
15     public ListNode find(ListNode p1,ListNode p2){
16         ListNode cur = p1, pre = null;
17         while(cur!=null && cur.val<=p2.val){
18             pre = cur;
19             cur = cur.next;
20         }
21         return pre;
22     }
23 }
View Code

Sort a linked list using insertion sort

Insertion Sort List

原文:http://www.cnblogs.com/krunning/p/3563992.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!