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的更多相关文章

  1. 【leetcode】Insertion Sort List (middle)

    Sort a linked list using insertion sort. 思路: 用插入排序对链表排序.插入排序是指每次在一个排好序的链表中插入一个新的值. 注意:把排好序的部分和未排序的部分 ...

  2. 【LeetCode】排序 sort(共20题)

    链接:https://leetcode.com/tag/sort/ [56]Merge Intervals (2019年1月26日,谷歌tag复习) 合并区间 Input: [[1,3],[2,6], ...

  3. 【leetcode】905. Sort Array By Parity

    题目如下: 解题思路:本题和[leetcode]75. Sort Colors类似,但是没有要求在输入数组本身修改,所以难度降低了.引入一个新的数组,然后遍历输入数组,如果数组元素是是偶数,插入到新数 ...

  4. 【链表】Insertion Sort List

    题目: Sort a linked list using insertion sort. 思路: 插入排序是一种O(n^2)复杂度的算法,基本想法相信大家都比较了解,就是每次循环找到一个元素在当前排好 ...

  5. 【Leedcode】Insertion Sort List

    Sort a linked list using insertion sort. /** * Definition for singly-linked list. * struct ListNode ...

  6. 【HackerRank】Insertion Sort Advanced Analysis(归并排序求数列逆序数对)

    Insertion Sort is a simple sorting technique which was covered in previous challenges. Sometimes, ar ...

  7. 【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 ...

  8. 【LeetCode】912. Sort an Array 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数排序 桶排序 红黑树排序 归并排序 快速排序 ...

  9. 【LeetCode】922. Sort Array By Parity II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用奇偶数组 排序 奇偶数位置变量 日期 题目地址: ...

随机推荐

  1. 从 React Router 谈谈路由的那些事

    React Router 是专为 React 设计的路由解决方案,在使用 React 来开发 SPA (单页应用)项目时,都会需要路由功能,而 React Router 应该是目前使用率最高的. Re ...

  2. js-控制浏览器和移动端的后退按钮 . popstate

    //控制浏览器和移动端的后退按钮 if (window.history && window.history.pushState) { $(window).on('popstate', ...

  3. ELK之收集haproxy日志

    由于HAProxy的运行信息不写入日志文件,但它依赖于标准的系统日志协议将日志发送到远程服务器(通常位于同一系统上),所以需要借助rsyslog来收集haproxy的日志.haproxy代理nginx ...

  4. Nginx+keepalived双机热备(主主模式)

    IP说明: master机器(master-node):10.0.0.5/172.16.1.5   VIP1:10.0.0.3slave机器(slave-node): 10.0.0.6/172.16. ...

  5. IntelliJ IDEA ,springboot 2.0 +mybatis 创建和访问数据库

    环境: JDK8+windows10 步骤 New Module —>Spring Initializr—>next 1 ​ 2. ​ 3.web勾选web,sql里面可以不勾,后续添加, ...

  6. eclipse 五种断点

    1. Line BreakpointLine Breakpoin是最简单的Eclipse断点,只要双击某行代码对应的左侧栏,就对该行设置上断点. 2. WatchpointLine Breakpoin ...

  7. MyBatis_SelectKey使用oracle 序列插入主键

    mapper 如下: 使用<selectkey>实现 也可以使用oracle的row 级触发器trigger实现: <?xml version="1.0" enc ...

  8. jsp、freemarker、velocity 的区别

    在java领域,表现层技术主要有三种:jsp.freemarker.velocity. 一.jsp是大家最熟悉的技术:优点:1.功能强大,可以写java代码2.支持jsp标签(jsp tag)3.支持 ...

  9. [置顶] vue-cli的webpack模板项目配置文件分析

    2017-09-11更新:更新到webpack 2.6.1所对应的配置,完善部分代码注释. 由于最近在vue-cli生成的webpack模板项目的基础上写一个小东西,开发过程中需要改动到build和c ...

  10. WPA2密钥重装攻击原理分析

    这两天最火爆的莫过 “WPA2被破解” 这一条大新闻了.我对其原理非常感兴趣,苦于没有找到的文献,所以就整理这么一篇,方便自己和大家理解.主要是根据目前发布的文章以及一些相关资料. 壹.WPA2的机制 ...