LeetCode: Insertion Sort List 解题报告
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 解题报告的更多相关文章
- 【LeetCode】147. Insertion Sort List 解题报告(Python)
[LeetCode]147. Insertion Sort List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- C#版 - LeetCode 148. Sort List 解题报告(归并排序小结)
leetcode 148. Sort List 提交网址: https://leetcode.com/problems/sort-list/ Total Accepted: 68702 Total ...
- 【LeetCode】Sort Colors 解题报告
[题目] Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- LeetCode——Insertion Sort List
LeetCode--Insertion Sort List Question Sort a linked list using insertion sort. Solution 我的解法,假设第一个节 ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
随机推荐
- Oslo 相机 App
https://itunes.apple.com/cn/app/osho/id1203312279?mt=8.它支持1:1,4:3,16:9多种分辨率拍摄,滤镜可在取景框的实时预览,拍摄过程可与滤镜实 ...
- 解析form表单数据
//解析form表单数据 function parseFormData(params) { var args = new Object(); for(var key in params){ if(!p ...
- 强制IE浏览器或WebBrowser控件
注册表: 32 bit: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BRO ...
- Spring-Boot原理及应用布署
一.Spring Boot的理念 从最根本上来讲,Spring Boot就是一些库的集合,它能够被任意项目的构建系统所使用.简便起见,该框架也提供了命令行界面,它可以用来运行和测试Boot应用.框架的 ...
- Twitter Bootstrap 中文帮助文档
http://wrongwaycn.github.io/bootstrap/docs/index.htmlTwitter Bootstrap 中文帮助文档 翻译得很不错~~~ 但是,还是要看英文文档 ...
- 字符集不同导致的ORA-00972
使用ssh登录RAC的两个节点,分别执行相同的sql语句,发现其中一个报错ora-00972: 检查后发现,左侧节点的客户端使用的字符集是SecureCRT的默认字符集,右侧使用的是UTF-8
- 【Android】图片切角,切指定的边。
公司的项目,UI和应用都是我自己做的.前几天设计了一个UI,出现了半边圆角的情况,如下图片所示.图片都来自服务器,肯定不能要求返回的图片按这个格式,必须在应用端对图片进行切角. Google了好久,发 ...
- Hadoop 读取文件API报错
Exception in thread "main" org.apache.hadoop.hdfs.BlockMissingException: Could not obtain ...
- oracle PLSQL 多结果集嵌套循环处理优化
oracle多结果集嵌套循环处理优化 --性能差 begin for a in (select id,name,sex,idcard from people) loop for b in (selec ...
- 使 Inno Setup 打包出的安装程序以管理员身份运行
找到 Inno Setup 安装目录下的 SetupLdr.e32 文件,用 Resource Hacker 将其中的 Manifest 修改一下: 改为: <requestedExecutio ...