题目链接

题目大意:对链表进行插入排序。

解法:直接插入排序。代码如下(耗时40ms):

     public ListNode insertionSortList(ListNode head) {
ListNode first = new ListNode(0);
ListNode pre = first, cur = head, post = null;
while(cur != null) {
//保存cur.next,因为要遍历当前结点,下一次就要遍历当前结点的下一个结点,所以在这次遍历完之后需要重新赋值cur=post
post = cur.next;
//寻找可以插入的结点位置
while(pre.next != null && pre.next.val < cur.val) {
pre = pre.next;
}
//找到之后,将cur结点插入在pre和pre.next之间
cur.next = pre.next;
pre.next = cur;
//下一次pre再从头开始找可插入的结点位置,所以要置为开始头节点
pre = first;
//下一次对cur.next结点进行排序,所以要将cur置回
cur = post;
}
return first.next;
}

147.Insertion Sort List---链表排序(直接插入)的更多相关文章

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

    插入排序的基本思想 把排好的放在一个新的变量中,每次拿出新的,排进去 这个新的变量要有超前节点,因为第一个节点可能会有变动 public ListNode insertionSortList(List ...

  2. LeetCode 147. Insertion Sort List 链表插入排序 C++/Java

    Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...

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

    Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...

  4. 147 Insertion Sort List 链表插入排序

    用插入排序对链表进行排序. 详见:https://leetcode.com/problems/insertion-sort-list/description/ Java实现: 链表的插入排序实现原理很 ...

  5. 【LeetCode】147. Insertion Sort List 解题报告(Python)

    [LeetCode]147. Insertion Sort List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...

  6. [LeetCode] Sort List 链表排序

    Sort a linked list in O(n log n) time using constant space complexity. 常见排序方法有很多,插入排序,选择排序,堆排序,快速排序, ...

  7. [LeetCode] Insertion Sort List 链表插入排序

    Sort a linked list using insertion sort. 链表的插入排序实现原理很简单,就是一个元素一个元素的从原链表中取出来,然后按顺序插入到新链表中,时间复杂度为O(n2) ...

  8. [leetcode sort]147. Insertion Sort List

    Sort a linked list using insertion sort. 利用插入排序对一个链表进行排序 思路和数组中的插入排序一样,不过每次都要从链表头部找一个合适的位置,而不是像数组一样可 ...

  9. Insertion Sort List——链表的插入排序

    Sort a linked list using insertion sort. 这道题跟 Sort List 类似,要求在链表上实现一种排序算法,这道题是指定实现插入排序.插入排序是一种O(n^2) ...

随机推荐

  1. Moya/RxSwift/ObjectMapper/Alamofire开发

    废话不多说直接上代码 // // MoyaNetWorking.swift // GreenAir // // Created by BruceAlbert on 2017/9/18. // Copy ...

  2. 【uoj#310】[UNR #2]黎明前的巧克力 FWT

    题目描述 给出 $n$ 个数,从中选出两个互不相交的集合,使得第一个集合与第二个集合内的数的异或和相等.求总方案数. 输入 第一行一个正整数 $n$ ,表示巧克力的个数.第二行 $n$ 个整数 $a_ ...

  3. 【bzoj3110】[Zjoi2013]K大数查询 整体二分+树状数组区间修改

    题目描述 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c.如果是2 a b c形式,表示询问从第a个位置到第b个位置,第C大的数 ...

  4. android面试(1)----布局

    1.说出android 五中布局,并说出各自作用? FrameLayout: 堆叠布局,也是就可以堆在一起.最长应用于Fragment的使用上. LinearLayout: 线性布局,可以是竖排或水平 ...

  5. robot framework Selenium2关键字介绍

    *** Settings *** Library Selenium2Library *** Keywords *** Checkbox应该不被选择 [Arguments] ${locator} Che ...

  6. BZOJ4951 Wf2017Money for Nothing(决策单调性)

    按时间排序,显然可能存在于答案中的公司价格应该单调递减.然后就可以大胆猜想感性证明其有决策单调性.具体地,设f(i,j)表示第i个消费公司和第j个生产公司搭配的获利,f(i,j)=(ti-tj)*(c ...

  7. [JLOI2012]时间流逝 树上高斯消元 概率期望

    题面 题意:(感觉题面写的题意是错的?)有\(n\)种能量不同的圈,设当前拥有的圈的集合为\(S\),则: 1,每天有\(p\)概率失去一个能量最小的圈.特别的,如果\(S = \varnothing ...

  8. Java高级应用之泛型与反射20170627

    /*************************************************************************************************** ...

  9. 最近遇到的DISCUZ一些问题解决方法

    “抱歉,您的请求来路不正确或表单验证串不符,无法提交” 打开“source\class\helper\helper_form.php”, 然后把“$_GET[‘formhash’] == formha ...

  10. js 多个事件的绑定及移除(包括原生写法和 jquery 写法)

    需要打开控制台查看效果: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...