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. 关于Red5整合springMVC提示scope not found 的错误

    https://www.cnblogs.com/qgc88:

  2. (40)C#windows服务控制

    ServiceController类 可以使用 ServiceController 类连接到现有服务并控制其行为. 构造函数 1.ServiceController () ServiceControl ...

  3. ML | k-means

    what's xxx k-means clustering aims to partition n observations into k clusters in which each observa ...

  4. socket的accept: Invalid argument问题

    void local_sdk_server::wait_remote_client_connect_and_comm() { /*服务器服务启动,等待客户端的链接的到来*/ //sockaddr_in ...

  5. POJ2104Kth Number

    整体二分模板题, 有些细节需要注意 #include<cstdio> #include<cctype> #include<climits> #include< ...

  6. GeoServer配置CORS(跨域资源共享)

    当前台页面请求WMS可能会遇到浏览器以下提示(浏览器控制台): 已阻止跨源请求:同源策略禁止读取位于 http://xxx.xxx.com 的远程资源.(原因:CORS 头缺少 'Access-Con ...

  7. filter 中用spring StopWatch 监控请求执行时间

    在filter中用spring stopWatch 来统计每个请求的执行时间: 虽然在firefox 中可以清楚的看到每个请求的执行时间,但是为了测试,记录日志, 方便以后查询维护. 还是必要的,下面 ...

  8. 简单过下bash/sh脚本基本知识吧

    1.sh/bash要注意空格的使用 2. for循环: for i in /etc/profile.d/*.sh ; do if [ -r "$i" ]; then if [ &q ...

  9. 【Todo】各种语言里面的for循环 & loop

    会的语言多了,不同语言的语法就会混淆.整理了一下. Java里面: 普通的for循环之外: 有以下格式: for (int x : intarr) { } JS里面: }; for (x in per ...

  10. 学习笔记 Java类的封装、继承和多态 2014.7.10

    1.问题:toString()没搞懂? int a = 1; Integer aa = new Integer(a); //这是实现的过程 System.out.println("Hello ...