【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--Reverse Nodes in k-Group **
http://oj.leetcode.com/problems/reverse-nodes-in-k-group/ 链表 指针 对链表翻转的变形 #include <iostream> ...
- 数据结构自己实现——stack
#define StackSize 100 typedef char DataType; class stack { public: DataType data[StackSize]; int top ...
- 2017-11-07-noip模拟题
T1 数学老师的报复 矩阵快速幂模板,类似于菲波那切数列的矩阵 [1,1]*[A,1 B,0] #include <cstdio> #define LL long long inline ...
- ML| EM
What's xxx The EM algorithm is used to find the maximum likelihood parameters of a statistical model ...
- Examples osgparticleshader例子学习
Examples osgparticleshader 粒子与shader的使用 参考文档 http://blog.csdn.net/csxiaoshui/article/details/234345 ...
- Spring核心(ioc控制反转)
IoC,Inversion Of Control 即控制反转,由容器来管理业务对象之间的依赖关系,而非传统方式中的由代码来管理. 其本质.即将控制权由应用程序代码转到了外部容器,控制权的转移就是 ...
- PS 如何使用抽出滤镜抠人物的头发丝等细节
1.打开图片,复制背景,关闭背景眼睛.单击 滤镜 -抽出, 我们要学会观察图片,先来看下面这张图: 这张图片色彩虽然不算丰富,但也不是纯色背景,甚至有些许的零乱,但人物的主题却被黑色长发包围, 我们只 ...
- spring secrity 一些常用小知识
1.在JSP页面获取当前登录的用户名的方法 首先引入taglib:<%@ taglib prefix="sec" uri="http://www.springfra ...
- Vue2.0 视频教程
好像是一套vue 开发webapp 课程.来自网络. url:https://pan.baidu.com/s/1jIele9w password:b404 文章来源:刘俊涛的博客 地址:http:// ...
- LINUX下目标文件的BSS段、数据段、代码段
http://blog.chinaunix.net/uid-27018250-id-3867588.html bss 未初始化的全局数据 data 已经初始化的全局数据 text 代码段,机器指令 r ...