首页 > 编程语言 > 详细

链表插入排序

时间:2016-01-21 23:57:48      阅读:422      评论:0      收藏:0      [点我收藏+]
/**
 * Definition for ListNode.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int val) {
 *         this.val = val;
 *         this.next = null;
 *     }
 * }
 */ 
public class Solution {
    /**
     * @param head: The first node of linked list.
     * @return: The head of linked list.
     */
    public ListNode insertionSortList(ListNode head) {
        // write your code here
        if(head == null){
            return null;
        }
        ListNode result = new ListNode(-1);
        ListNode pre = result;
        ListNode cur = head;
        while(cur!= null){
            ListNode next = cur.next;
            pre = result;
            while(pre.next!=null&& pre.next.val<=cur.val){
                pre = pre.next;
            }
            cur.next = pre.next;
            pre.next = cur;
            cur = next;
        }
        return result.next;
    }
}

  

链表插入排序

原文:http://www.cnblogs.com/wangnanabuaa/p/5149730.html

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