[Linked List]Insertion Sort List
Sort a linked list using insertion sort.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* insertionSortList(ListNode* head) {
if(head==NULL || head->next==NULL){
return head;
}
ListNode* cur = head->next,*cur_pre=head,*cur_next=NULL;
while(cur){
cur_next = cur->next; /* 当前值小于前驱值,该节点需要重新调整 */
if(cur->val < cur_pre->val){
ListNode* insert_node_pre = NULL,*insert_node=head;
while(cur->val > insert_node->val){
insert_node_pre = insert_node;
insert_node = insert_node->next;
}
insert_node_pre ? insert_node_pre->next = cur : head = cur;
cur->next = insert_node;
cur_pre->next = cur_next;
}else{
cur_pre = cur;
} cur = cur_next;
}
return head;
}
};
[Linked List]Insertion Sort List的更多相关文章
- [LeetCode] Insertion Sort List 链表插入排序
Sort a linked list using insertion sort. 链表的插入排序实现原理很简单,就是一个元素一个元素的从原链表中取出来,然后按顺序插入到新链表中,时间复杂度为O(n2) ...
- leetcode Insertion Sort List
题目:Sort a linked list using insertion sort. 代码: /** * Definition for singly-linked list. * struct Li ...
- 【leetcode】Insertion Sort List (middle)
Sort a linked list using insertion sort. 思路: 用插入排序对链表排序.插入排序是指每次在一个排好序的链表中插入一个新的值. 注意:把排好序的部分和未排序的部分 ...
- 9. Sort List && Insertion Sort List (链表排序总结)
Sort List Sort a linked list in O(n log n) time using constant space complexity. H ...
- LeetCode OJ 147. Insertion Sort List
Sort a linked list using insertion sort. Subscribe to see which companies asked this question 解答 对于链 ...
- Java for LeetCode 147 Insertion Sort List
Sort a linked list using insertion sort. 解题思路: 插入排序,JAVA实现如下: public ListNode insertionSortList(List ...
- 【LeetCode OJ】Insertion Sort List
Problem: Sort a linked list using insertion sort. The node of the linked list is defined as: /** * D ...
- 147. Insertion Sort List
Sort a linked list using insertion sort. 代码如下: /** * Definition for singly-linked list. * public cla ...
- leetcode 147. Insertion Sort List ----- java
Sort a linked list using insertion sort. 插入排序. /** * Definition for singly-linked list. * public cla ...
随机推荐
- Xcode 中添加 .pch文件
1 新建工程 2 创建 .pch文件 3 在setting里面进行设置:
- contentSize、contentInset和contentOffset
contentSize.contentInset和contentOffset 是 scrollView三个基本的属性. contentSize: The size of the content vie ...
- 已知TSP问题的最好解
a280 : 2579ali535 : 202339att48 : 10628att532 : 27686bayg29 : 1610bays29 : 2020berlin52 : 7542bier12 ...
- [转载]启用 VIM 中的 Python 自动补全及提示功能
转载: http://zhongwei-leg.iteye.com/blog/941474 周围的同事不喜欢使用 VIM 写 Python 代码的原因之一就是,VIM 不能像 Visual Studi ...
- select2使用详解
官网: https://select2.github.io/examples.html 引用: <link href="~/Scripts/select2/select2.css&qu ...
- 利用树莓派跑python爬虫的简单教程——从无到有
因为学校项目的原因入手了树莓派,到手先折腾了两天,发现网上的教程大都是拿他搭建服务器,mail,或者媒体服务器之类,对于在学校限时的宽带来说有点不太现实,不过低功耗适合一直开着的确启发了我.所以想到拿 ...
- QThread与其他线程间相互通信
转载请注明链接与作者huihui1988 QThread的用法其实比较简单,只需要派生一个QThread的子类,实现其中的run虚函数就大功告成, 用的时候创建该类的实例,调用它的start方法即可. ...
- C语言的本质(33)——GCC编译器入门
GCC(GNU CompilerCollection,GNU编译器套装),是由 GNU 开发的编程语言编译器.它是以GPL许可证所发行的自由软件,也是 GNU计划的关键部分.GCC原本作为GNU操作系 ...
- bzoj1643 [Usaco2007 Oct]Bessie's Secret Pasture 贝茜的秘密草坪
Description 农夫约翰已经从他的牧场中取得了数不清块数的正方形草皮,草皮的边长总是整数(有时农夫约翰割草皮的刀法不合适,甚至切出了边长为0的正方形草皮),他已经把草皮放在了一个奶牛贝茜已经知 ...
- 添加python第三方插件时出现的问题
当我安装beautifulsoup4时出现了如下错误: Fatal error in launcher: Unable to create process using '""F:\ ...