[LeetCode] 147. Insertion Sort List 链表插入排序
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:
- Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list.
- 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.
- 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
链表的插入排序实现原理很简单,就是一个元素一个元素的从原链表中取出来,然后按顺序插入到新链表中,时间复杂度为 O(n2),是一种效率并不是很高的算法,但是空间复杂度为 O(1),以高时间复杂度换取了低空间复杂度,参见代码如下:
class Solution {
public:
ListNode* insertionSortList(ListNode* head) {
ListNode *dummy = new ListNode(-), *cur = dummy;
while (head) {
ListNode *t = head->next;
cur = dummy;
while (cur->next && cur->next->val <= head->val) {
cur = cur->next;
}
head->next = cur->next;
cur->next = head;
head = t;
}
return dummy->next;
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/147
类似题目:
Insert into a Cyclic Sorted List
参考资料:
https://leetcode.com/problems/insertion-sort-list/
https://leetcode.com/problems/insertion-sort-list/discuss/46423/Explained-C%2B%2B-solution-(24ms)
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] 147. Insertion Sort List 链表插入排序的更多相关文章
- LeetCode 147. Insertion Sort List 链表插入排序 C++/Java
Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...
- 147 Insertion Sort List 链表插入排序
用插入排序对链表进行排序. 详见:https://leetcode.com/problems/insertion-sort-list/description/ Java实现: 链表的插入排序实现原理很 ...
- [LeetCode]147. Insertion Sort List链表排序
插入排序的基本思想 把排好的放在一个新的变量中,每次拿出新的,排进去 这个新的变量要有超前节点,因为第一个节点可能会有变动 public ListNode insertionSortList(List ...
- [LeetCode] Insertion Sort List 链表插入排序
Sort a linked list using insertion sort. 链表的插入排序实现原理很简单,就是一个元素一个元素的从原链表中取出来,然后按顺序插入到新链表中,时间复杂度为O(n2) ...
- [LeetCode] 147. Insertion Sort List 解题思路
Sort a linked list using insertion sort. 问题:实现单向链表的插入排序. 这是比较常规的一个算法题目. 从左往右扫列表,每次将指针的下一个元素插入前面已排好序的 ...
- Java for LeetCode 147 Insertion Sort List
Sort a linked list using insertion sort. 解题思路: 插入排序,JAVA实现如下: public ListNode insertionSortList(List ...
- leetcode 147. Insertion Sort List ----- java
Sort a linked list using insertion sort. 插入排序. /** * Definition for singly-linked list. * public cla ...
- Leetcode#147 Insertion Sort List
原题地址 心得:有关链表的题目,多用中间变量,代码写得清晰一点,适当注释 代码: ListNode *insertionSortList(ListNode *head) { if (!head) re ...
- 【数据结构】算法 LinkList (Insertion Sort List 链表插入排序)
将一个单链表进行处理后,所得结果为一有序链表 Solution: 将原始链表逐个查询,插入新链表,在插入的同时对链表进行排序.时间复杂度O(n*n) public ListNode insertion ...
随机推荐
- JS解决所有浏览器连续输入英文字母不换行问题,包括火狐(转)
问题描述: <p style="font-size:12px;line-height:30px;">测试数据测试数据</p> p标签内如果输入一长段英文字符 ...
- Hyper V NAT 网络设置 固定IP / DHCP
Hyper V 默认的Default Switch同时支持了NAT网络以及DHCP,虚拟机能够访问外网. 但使用过程中发现这个IP网段经常变化,而且Hyper V没有提供管理其NAT网络与DHCP的图 ...
- 重温CLR(十七)程序集加载和反射
本章主要讨论在编译时对一个类型一无所知的情况下,如何在运行时发现类型的信息.创建类型的实例以及访问类型的成员.可利用本章讲述的内容创建动态可扩展应用程序. 反射使用的典型场景一般是由一家公司创建宿主应 ...
- MySQL入门——在Windows下安装MySQL
MySQL入门——在Windows下安装MySQL 摘要:本文主要说明了如何下Windows环境下安装MySQL. 查看电脑上是否安装了MySQL 打开cmd窗口,输入 services.msc 命令 ...
- shiro加密算法
第一节的时候我介绍过,shiro有很多加密算法,如md5和sha,而且还支持加盐,使得密码的解析变得更有难度,更好的保障了数据的安全性. 这里我们要介绍的是md5算法,因为比较常用.首先我们来看看md ...
- maven 学习---Maven自动化部署
在项目开发中,通常是部署过程包含以下步骤 检入代码在建项目全部进入SVN或源代码库中,并标记它. 从SVN下载完整的源代码. 构建应用程序. 生成输出要么WAR或EAR文件存储到一个共同的网络位置. ...
- pid相关命令
pidof 查找正在运行进程的进程号(pid)的工具 pidof - find the process ID of a running program 参数: -s 表示只返回1个 pid -x 表示 ...
- 一个简单servlet容器
一个简单servlet容器 2.1 javax.servlet.Servlet接口 Servlet编程需要使用javax.servlet和javax.servlet.http两个包下的接口和类 在所有 ...
- linux驱动学习笔记---实现中断下半部以及驱动编写规范(七)【转】
转自:https://blog.csdn.net/weixin_42471952/article/details/81609141 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协 ...
- win10上安装ubunt18双系统过程中出现mmx64.efi not found问题
安装Ubuntu18过程中,从u盘启动ubunt安装,出现mmx64.efi not found问题 如下图: 制作好ubunt启动盘之后在EFI/BOOT下会看到两个文件,将其中grubx64.ef ...