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

[LeetCode]147. Insertion Sort List链表排序

时间:2018-02-12 15:11:08      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:tco   ext   tno   code   blog   好的   gpo   etc   bsp   

插入排序的基本思想

把排好的放在一个新的变量中,每次拿出新的,排进去

这个新的变量要有超前节点,因为第一个节点可能会有变动

public ListNode insertionSortList(ListNode head) {
        if (head==null||head.next==null) return head;
        ListNode dummy = new ListNode(0);
        ListNode h = head;
        ListNode next;
        ListNode temp;
        while (h!=null)
        {
            next = h.next;
            temp = dummy;
            while (temp.next!=null&&h.val>temp.next.val) {
                temp = temp.next;
            }
            h.next = temp.next;
            temp.next = h;
            h = next;
        }
      

 

[LeetCode]147. Insertion Sort List链表排序

标签:tco   ext   tno   code   blog   好的   gpo   etc   bsp   

原文地址:https://www.cnblogs.com/stAr-1/p/8444336.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!