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. HDU 3032 Nim or not Nim? (sg函数)

    Nim or not Nim? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  2. php支付宝开发

    1.下载对应的接口demo,注意退款有无密和有密 2.根据接口代码编写,要注意notify_url和return_url不能带有额外参数,并且notify_url中途不能含有跳转,否则post数据会丢 ...

  3. 【HTML】WWW简介

    www 什么是WWW www(world wide web),又称为万维网,或通常称为web,是一个基于超文本方式的信息检索服务工具. WWW的工作模式 C/S结构(client/server结构), ...

  4. 【struts2】名为chain的ResultType

    1)基本使用 名称为“chain”的ResultType,在struts-default.xml里的配置如下: <result-type name="chain" class ...

  5. Python学习笔记001——Linux

    Linux文件系统采用树形目录结构,系统中一切皆文件.文件名字母区分大小写 Linux命令使用格式(终端窗口) 命令名 [选项] [参数] 命令名:在命令行输入命令. 备注:命令名字母区分大小写, 1 ...

  6. Oracle 12C -- ADRCI查看DDL日志

    $ adrci ADRCI: Release - Production on Tue Nov :: Copyright (c) , , Oracle and/or its affiliates. Al ...

  7. 【转】Markdown 的一些问题

    Markdown 的一些问题 把我之前的博文基本上转换成了 markdown 格式.我发现 markdown 虽然在编辑器里看起来比 HTML 清晰一些,但也有一些不足. 这些 markup 语言的格 ...

  8. ftk学习记(滑动条篇)

    [声明:版权全部,欢迎转载,请勿用于商业用途. 联系信箱:feixiaoxing @163.com] 前一篇说到了combox.就先看一下效果吧. 说完了combox,就谈谈今天的滑动条.滑动条,当然 ...

  9. CentOS下Redisserver安装配置

    1.CentOS 6.6下Redis安装配置记录 2.CentOS下Redisserver安装配置

  10. 7.翻译:EF基础系列---EF中的实体类型

    原文地址:http://www.entityframeworktutorial.net/Types-of-Entities.aspx 在Entity Framework中有两种实体类型:一种是POCO ...