Sort a linked list using insertion sort.


A graphical example of insertion sort. The partial sorted list (black) initially contains only the first element in the list.
With each iteration one element (red) is removed from the input data and inserted in-place into the sorted list

Algorithm of Insertion Sort:

  1. Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list.
  2. At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there.
  3. It repeats until no input elements remain.

Example 1:

Input: 4->2->1->3
Output: 1->2->3->4

Example 2:

Input: -1->5->3->4->0
Output: -1->0->3->4->5 Solution:
  就是简单的插入算法
 class Solution {
public:
ListNode *insertionSortList(ListNode *head) {
if (head == nullptr || head->next == nullptr)return head;
ListNode *durry, *p, *pre, *cur, *next;
durry = new ListNode(-);
durry->next = head;
p = pre = cur = next = head;
next = cur->next;
while (next != nullptr)
{
cur = next;
next = cur->next;
p = durry;
while (p != cur)
{
if (p->next->val > cur->val)
{
pre->next = next;
cur->next = p->next;
p->next = cur;
break;
}
p = p->next;
}
if (pre->next == cur)//未移动过
pre = cur;
}
return durry->next;
}
};

力扣算法题—147Insertion_Sort_List的更多相关文章

  1. 力扣算法题—069x的平方根

    实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去. 示例 1: 输入: 4 输出: 2 示例 ...

  2. 力扣算法题—060第K个排列

    给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: "123" "132&qu ...

  3. 力扣算法题—050计算pow(x, n)

    #include "000库函数.h" //使用折半算法 牛逼算法 class Solution { public: double myPow(double x, int n) { ...

  4. 力扣算法题—093复原IP地址

    给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式. 示例: 输入: "25525511135" 输出: ["255.255.11.135", ...

  5. 力扣算法题—079单词搜索【DFS】

    给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字母不允许被重复使用. ...

  6. 力扣算法题—052N皇后问题2

    跟前面的N皇后问题没区别,还更简单 #include "000库函数.h" //使用回溯法 class Solution { public: int totalNQueens(in ...

  7. 力扣算法题—051N皇后问题

    #include "000库函数.h" //使用回溯法来计算 //经典解法为回溯递归,一层一层的向下扫描,需要用到一个pos数组, //其中pos[i]表示第i行皇后的位置,初始化 ...

  8. 力扣算法题—143ReorderList

    Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You may not mod ...

  9. 力扣算法题—144Binary Tree Preorder Traversal

    Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1,null,2,3 ...

随机推荐

  1. Bootstrap 学习笔记2 栅格系统 辅助类下拉框

    辅助类和响应式工具: 颜色和字体相同 响应式工具: 图标菜单按钮组件: btn-group 按钮式下拉菜单

  2. multiprocessing的Process类的简单使用

    ''' 跨平台的进程创建模块(multiprocessing) 支持跨平台 :window/linux multiprocessing提供一个Process类来代表一个进程对象 ''' from mu ...

  3. Spring IOC DI AOP 的简单理解及应用

    Spring两大特性:IOC 和AOP.IOC 控制反转,AOP 面向切面编程 spring 核心容器的主要组件时Bean工厂(BeanFactory) ,Bean 工厂使用控制反转模式来降低程序代码 ...

  4. JavaFx开发桌面软件

    JavaFx开发桌面软件 */--> code {color: #FF0000} pre.src {background-color: #002b36; color: #839496;} Jav ...

  5. 多线性方程组迭代算法——Gauss-Seidel迭代算法的Python实现

    多线性方程组(张量)迭代算法的原理请看这里:原理部分请留言,不方便公开分享 Jacobi迭代算法里有详细注释:多线性方程组迭代算法——Jacobi迭代算法的Python实现 import numpy ...

  6. python 字典(dictionary)一些方法

    1.python 字典(Dictionary) keys() 函数以列表返回一个字典所有的键. keys()语法: dict.keys() 2.setdefault()方法 python字典setde ...

  7. 强烈推荐一款功能强大的Tomcat 管理监控工具

    专注于Java领域优质技术号,欢迎关注 原创: 侯树成 Tomcat那些事儿 启动 Tomcat完毕 ,有些时候总会打开浏览器 http://localhost:8080/ 去验证你的Tomcat是否 ...

  8. c# 使用网站的身份验证及 Cookie 的获取与使用

    C# 的 Http 访问可以使用 .net 自带的  HttpWebRequest, WebClient, HttpClient 类.也可以使用开源库 RestSharp . RestSharp 的优 ...

  9. go读写excel文件

    首先,需要安装golang用来操作excel文档的类库: go get github.com/Luxurioust/excelize 一.excel文件创建与写入 package main impor ...

  10. Springboot整合Hikari数据库连接池,密码加密

    1.application.yml配置 spring: datasource: jdbcUrl: jdbc:mysql://127.0.0.1:3306/jby?serverTimezone=UTC& ...