Insertion Sort List

Sort a linked list using insertion sort.

SOLUTION:

使用一个dummynode 创建一个新的链,将旧的节点插入到新链中即可,相当简单哦!

 /**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode insertionSortList(ListNode head) {
ListNode dummy = new ListNode(0); while (head != null) {
ListNode pre = dummy; // 注意,这里要用<= 来保证算法的稳定性
// 因为假如有2个数相同,后面的数后找到,也要插入到后面才可以。也就是说当=的时候,是继续往下走
while (pre.next != null && pre.next.val <= head.val) {
pre = pre.next;
} // unlink the node from the original link. And record the next position.
ListNode headNext = head.next;
head.next = pre.next; pre.next = head;
head = headNext;
} return dummy.next;
}
}

GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/list/InsertionSortList.java

LeetCode: Insertion Sort List 解题报告的更多相关文章

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

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

  2. C#版 - LeetCode 148. Sort List 解题报告(归并排序小结)

    leetcode 148. Sort List 提交网址: https://leetcode.com/problems/sort-list/  Total Accepted: 68702 Total ...

  3. 【LeetCode】Sort Colors 解题报告

    [题目] Given an array with n objects colored red, white or blue, sort them so that objects of the same ...

  4. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  5. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  6. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  7. LeetCode——Insertion Sort List

    LeetCode--Insertion Sort List Question Sort a linked list using insertion sort. Solution 我的解法,假设第一个节 ...

  8. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  9. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

随机推荐

  1. lsof命令详解(转)

    lsof命令详解(转) 上一篇 / 下一篇  2011-06-09 21:56:41 / 个人分类:Linux 查看( 351 ) / 评论( 0 ) / 评分( 0 / 0 ) 在Linux中,ls ...

  2. 自动化部署必备技能—搭建YUM仓库

    导言: YUM主要用于自动安装.升级rpm软件包,它能自动查找并解决rpm包之间的依赖关系.要成功的使用YUM工具安装更新软件或系统,就需要有一个包含各种rpm软件包的repository(软件仓库) ...

  3. 神文章2:文本矩阵简述 V1.0 -vivo神人

    评论: 牛逼的业余书籍爱好者读书思路,指导思想. 2013/9/22         文本矩阵简述 V1.0http://www.douban.com/note/170688812/ 文/vivo   ...

  4. 进阶之路(中级篇) - 015 串口控RGB三色灯

    本文由博主原创,如有不对之处请指明,转载请说明出处. /********************************* 代码功能:串口控RGB三色灯 使用函数: Serial.flush(); / ...

  5. System.Web.HttpValueCollection.ThrowIfMaxHttpCollectionKeysExceeded

    昨天客户跟我说,突然一个页面频繁地报ThrowIfMaxHttpCollectionKeysExceeded这个异常.而且是数据量大的时候报错,数据量小的时候OK. 根据异常的名称也能看得差不多超过了 ...

  6. /struts-tags not found ,/struts-dojo-tags not found 上线后异常解决方案

    上线到2003上后发现2个问题:1 缺少/struts-tags2 缺少/struts-dojo-tags在xp上不用直接指定这些文件的位置,但在其他的系统可能无法自动找到它的路径,一定要明确指定在w ...

  7. 【C语言】练习1-22

     题目来源:<The C programming language>中的习题  练习1-22:编写一个程序,把较长的输入行‘折’成短一些的两行或者多行,折行的位置在输入行的第n列之前的最后 ...

  8. Android中Is library配置的作用

    在Android开发中如果用eclipse开发的话,在配置的时候会有一个选项,Is library一直没有研究明白,经过上网查找,有人归纳了用法,归纳的很好作为保留.解决了我多个项目共享资源的好方法. ...

  9. Ubuntu 14.04 静态IP设置, ping通win7

    ubuntu 14.04以后不支持 /etc/init.d/networing restart命令,使用ifdown et0 和ifup eth0 使用echo $?查看上一次命令执行的结果 ifdo ...

  10. centos7 安装后需要做的事情

    安装centos 7 系统之后要做的几件事 #修改主机名 hostnamectl --static set-hostname xd-1 vim /etc/hosts127.0.0.1 xd-1x.x. ...