[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 ...
随机推荐
- Flask-Moment本地化日期和时间
moment.js客户端开源代码库,可以在浏览器中渲染日期和时间.Flask-Moment是一个flask程序扩展,能把moment.js集成到Jinja2模板中. 1.安装 pip install ...
- background属性怎么添加2个或多个背景图
最近遇到一个需求,下面充值金额按钮是一个背景图,点击之后显示的状态也是一个背景图,如下图 按照惯用的套路,新增一个class,点击后的状态直接写在里面即可 然而点击后,虽然状态背景成功显示出 ...
- hdu-5573 Binary Tree
The Old Frog King lives on the root of an infinite tree. According to the law, each node should conn ...
- Window权限维持(三):服务
如果未正确配置Windows环境中的服务或这些服务可以用作持久性方法,则这些服务可能导致权限提升.创建一个新的服务需要管理员级别的特权,它已经不是隐蔽的持久性技术.然而,在红队的行动中,针对那些在威胁 ...
- 转 经典分类网络Googlenet
转自https://my.oschina.net/u/876354/blog/1637819 2014年,GoogLeNet和VGG是当年ImageNet挑战赛(ILSVRC14)的双雄,GoogLe ...
- QT+OpenGL(01)--实现三角形渲染
1.openglwidget.ui <ui version="4.0"> <author/> <comment/> <exportmacr ...
- Python基础24
import 与 from import 知乎上说的简洁明了,zhihu.com/question/38857862 from import, 导入之后就能拿来用了,直接用!到处用!
- HTML input属性详谈
value属性 value属性指定输入字段的初始值: <form> 名字:<br> <input type="text" name="you ...
- C++优先队列例子
#pragma GCC optimize(3) #include <bits/stdc++.h> #define N 105 using namespace std; struct Nod ...
- leetcode - 括号字符串是否有效
括号字符串是否有效 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭合. ...