【LeetCode】Insertion Sort List
Sort a linked list using insertion sort.
//用到O(N)的额外空间
public class Solution {
public ListNode insertionSortList(ListNode head) {
if(head==null||head.next==null)
return head;
ListNode root = new ListNode(head.val);
ListNode cur = head.next;
while(cur!=null){
ListNode tempNode = root;
ListNode pre = root;
while(tempNode!=null){
if(cur.val>tempNode.val){
pre=tempNode;
tempNode=tempNode.next;
}else{
break;
}
}
if(tempNode==root){
ListNode newNode = new ListNode(cur.val);
newNode.next=root;
root=newNode;
cur=cur.next;
}else if(tempNode==null){
ListNode newNode = new ListNode(cur.val);
pre.next=newNode;
cur=cur.next;
}else{
ListNode newNode = new ListNode(cur.val);
pre.next=newNode;
newNode.next=tempNode;
cur=cur.next;
} } return root; }
}
public class NSolution {
public ListNode insertionSortList(ListNode head) {
if(head==null||head.next==null)
return head;
ListNode cur = head.next;
head.next=null;
while(cur!=null){
ListNode tempNode = head;
ListNode pre = head;
while(tempNode!=null){
if(cur.val>tempNode.val){
pre=tempNode;
tempNode=tempNode.next;
}else{
break;
}
}
if(tempNode==head){
ListNode newNode = new ListNode(cur.val);
newNode.next=head;
head=newNode;
cur=cur.next;
}else if(tempNode==null){
ListNode newNode = new ListNode(cur.val);
pre.next=newNode;
cur=cur.next;
}else{
ListNode newNode = new ListNode(cur.val);
pre.next=newNode;
newNode.next=tempNode;
cur=cur.next;
}
}
return head;
}
}
【LeetCode】Insertion Sort List的更多相关文章
- 【leetcode】Insertion Sort List (middle)
Sort a linked list using insertion sort. 思路: 用插入排序对链表排序.插入排序是指每次在一个排好序的链表中插入一个新的值. 注意:把排好序的部分和未排序的部分 ...
- 【LeetCode】排序 sort(共20题)
链接:https://leetcode.com/tag/sort/ [56]Merge Intervals (2019年1月26日,谷歌tag复习) 合并区间 Input: [[1,3],[2,6], ...
- 【leetcode】905. Sort Array By Parity
题目如下: 解题思路:本题和[leetcode]75. Sort Colors类似,但是没有要求在输入数组本身修改,所以难度降低了.引入一个新的数组,然后遍历输入数组,如果数组元素是是偶数,插入到新数 ...
- 【链表】Insertion Sort List
题目: Sort a linked list using insertion sort. 思路: 插入排序是一种O(n^2)复杂度的算法,基本想法相信大家都比较了解,就是每次循环找到一个元素在当前排好 ...
- 【Leedcode】Insertion Sort List
Sort a linked list using insertion sort. /** * Definition for singly-linked list. * struct ListNode ...
- 【HackerRank】Insertion Sort Advanced Analysis(归并排序求数列逆序数对)
Insertion Sort is a simple sorting technique which was covered in previous challenges. Sometimes, ar ...
- 【LeetCode】75. Sort Colors (3 solutions)
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
- 【LeetCode】912. Sort an Array 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数排序 桶排序 红黑树排序 归并排序 快速排序 ...
- 【LeetCode】922. Sort Array By Parity II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用奇偶数组 排序 奇偶数位置变量 日期 题目地址: ...
随机推荐
- LeetCode OJ——Word Ladder
http://oj.leetcode.com/problems/word-ladder/ 图的最短路径问题,可以用最短路径算法,也可以深搜,也可以广搜. 深搜版本: 第一次写的时候,把sum和visi ...
- AC日记——【模板】线段树 1 洛谷 P3372
题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数加上x 2.求出某区间每一个数的和 输入输出格式 输入格式: 第一行包含两个整数N.M,分别表示该数列数字的个数和操作的总个 ...
- 使用 dotnet CLI 来打包和发布 .NET Core nuget package
原文链接:使用 dotnet CLI 来打包和发布 .NET Core nuget package 如何使用 visual studio 2015/2017 打包和发布 Nuget package, ...
- sql server 博客
http://blog.csdn.net/tjvictor/article/category/531421/1 http://blog.csdn.net/zhangqidashu/article/de ...
- 利用NSString的Hash方法比较字符串
实际编程总会涉及到比较两个字符串的内容,一般会用 [string1 isEqualsToString:string2] 来比较两个字符串是否一致.对于字符串的isEqualsToString方法,需要 ...
- 【hibernate】Hibernate中get()和load()的区别
Hibernate中根据Id单条查询获取对象的方式有两种,分别是get()和load(),来看一下这两种方式的区别. 1. get() 使用get()来根据ID进行单条查询: 1 User user= ...
- 彻底搞懂oracle的标量子查询
oracle标量子查询和自己定义函数有时用起来比較方便,并且开发者也常常使用.数据量小还无所谓.数据量大,往往存在性能问题. 下面測试帮助大家彻底搞懂标量子查询. SQL> create tab ...
- 【matlab】:matlab中不断的出现计算过程怎么办
这个问题是会常常性出的.就是matlab中不断的出现计算. 关于这个问题,我们须要考虑的是自己是不是写错了,通常会出现以下两种可能的错误 1,关于计算的函数没有写分号 :这样的是致命问题,假设函数不写 ...
- C 标准库 - <signal.h>
C 标准库 - <signal.h> 简介 signal.h 头文件定义了一个变量类型 sig_atomic_t.两个函数调用和一些宏来处理程序执行期间报告的不同信号. 库变量 下面是头文 ...
- Eclipse 安装(Oxygen版本)
Eclipse 安装(Oxygen版本) Eclipse 最新版本 Eclipse Neon,这个首次鼓励用户使用 Eclipse Installer 来做安装,这是一种由Eclipse Oomph提 ...