码迷,mamicode.com
首页 > 编程语言 > 详细

链表插入排序

时间:2016-01-21 23:58:14      阅读:434      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!