LeetCode 147. Insertion Sort List 链表插入排序 C++/Java
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: ->->->
Output: ->->->
Example 2:
Input: -->->->->
Output: -->->->->
解题思路:将原链表中元素一个一个取出来,在新链表中比较进行排序。时间复杂度为O(n2),是一种效率并不是很高的算法,但是空间复杂度为O(1),以高时间复杂度换取了低空间复杂度。
方法一(C++)
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;
}
(Java)
public ListNode insertionSortList(ListNode head) {
ListNode dummy=new ListNode(-1),cur=dummy;
while(head!=null){
ListNode t=head.next;
cur=dummy;
while(cur.next!=null&&cur.next.val<=head.val)
cur=cur.next;
head.next=cur.next;
cur.next=head;
head=t;
}
return dummy.next;
}
方法二:不符合题目中的要求,只是完成最基本的链表排序功能,借助vector中的sort排序方法。(C++)
ListNode* insertionSortList(ListNode* head) {
vector<int> m;
while(head){
m.push_back(head->val);
head=head->next;
}
sort(m.begin(),m.end());
ListNode* newhead=new ListNode(-);
while(!m.empty()){
ListNode* cur=new ListNode(m.back());
m.pop_back();
cur->next=newhead->next;
newhead->next=cur;
}
return newhead->next;
}
LeetCode 147. Insertion Sort List 链表插入排序 C++/Java的更多相关文章
- [LeetCode] 147. Insertion Sort List 链表插入排序
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 ...
随机推荐
- 运行Tomcat报错 解决方法
The APR based Apache Tomcat Native library which allows optimal performance in production environmen ...
- 解决win7无法运行bat批处理文件的方法
在win7系统中我们可以将一些命令制作为bat批处理文件,只需双击打开即可运行命令,方便使用. 那么,要怎么运行bat批处理呢?最近有用户反馈,遇到无法运行bat批处理的现象,该怎么办呢? 修复方法一 ...
- 20164301 Exp3 免杀原理与实践
Exp3 免杀原理与实践 1. 实践内容 1.1 正确使用msf编码器,msfvenom生成如jar之类的其他文件,veil-evasion,加壳工具,使用shellcode编程 1.2 通过组合应 ...
- Python-100道练习题答案
题目链接:http://www.runoob.com/python/python-100-examples.html #5. # def find_max(): # a=int(input(" ...
- rsync命令简单用法介绍
rsync有两种常用的认证方式,一种为rsync-daemon方式,另外一种则是ssh.在一些场合,使用rsync-daemon方式会比较缺乏灵活性,ssh方式则成为首选.但是今天实际操作的时候发现当 ...
- js 中逻辑为 false 的8种情况
如果对象无初始值或者其值为 数字0.-0.null."".false.undefined 或者 NaN,那么对象的逻辑值为 false. 注意:字符串 '0',值为 true ty ...
- 1.1.27 word表格里的文字不显示
1.问题: 下载其他人做的表格后,在表格内打字,字不显示. 2.解决方案: 产生这种问题的原因是,该表格设置的字体,你的电脑未安装. a.将隐藏文字选中,设为[宋体]或其他已经安装字体. b.下载[方 ...
- IDEA 控制台乱码问题
一.修改IDEA的配置文件 找到IDEA安装目录下的 /bin/idea.exe.vmoptions(32位) 或 idea64.exe.vmoptions(64位),添加如下一行: -Dfile.e ...
- 解决spyder、Jupyter Notebook 打不开
参考: https://blog.csdn.net/lanchunhui/article/details/72891918 https://stackoverflow.com/questions/49 ...
- Azure CosmosDB (9) Unique Key Constraints
<Windows Azure Platform 系列文章目录> 在Azure Cosmos DB中,还支持Unique Key Constraints(唯一键约束). 我们可以在Azure ...