【Leedcode】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) {
ListNode dummy(INT_MIN);
ListNode *cur = head;
while (cur != nullptr) {
ListNode *pos = findInsertPos(&dummy, cur->val);
ListNode *temp = cur->next;
cur->next = pos->next;
pos->next = cur;
cur = temp;
}
return dummy.next;
}
private:
ListNode *findInsertPos(ListNode *before_head, int key) {
ListNode *pre = nullptr, *cur = before_head;
while (cur != nullptr && cur->val <= key) {
pre = cur;
cur = cur->next;
}
return pre;
}
};
【Leedcode】Insertion Sort List的更多相关文章
- 【leetcode】Insertion Sort List (middle)
Sort a linked list using insertion sort. 思路: 用插入排序对链表排序.插入排序是指每次在一个排好序的链表中插入一个新的值. 注意:把排好序的部分和未排序的部分 ...
- 【链表】Insertion Sort List
题目: Sort a linked list using insertion sort. 思路: 插入排序是一种O(n^2)复杂度的算法,基本想法相信大家都比较了解,就是每次循环找到一个元素在当前排好 ...
- 【HackerRank】Insertion Sort Advanced Analysis(归并排序求数列逆序数对)
Insertion Sort is a simple sorting technique which was covered in previous challenges. Sometimes, ar ...
- 【LeetCode】Insertion Sort List
Sort a linked list using insertion sort. //用到O(N)的额外空间 public class Solution { public ListNode inser ...
- 【LeetCode】排序 sort(共20题)
链接:https://leetcode.com/tag/sort/ [56]Merge Intervals (2019年1月26日,谷歌tag复习) 合并区间 Input: [[1,3],[2,6], ...
- 【leetcode】905. Sort Array By Parity
题目如下: 解题思路:本题和[leetcode]75. Sort Colors类似,但是没有要求在输入数组本身修改,所以难度降低了.引入一个新的数组,然后遍历输入数组,如果数组元素是是偶数,插入到新数 ...
- 【LeetCode OJ】Insertion Sort List
Problem: Sort a linked list using insertion sort. The node of the linked list is defined as: /** * D ...
- 【leetcode刷题笔记】Insertion Sort List
Sort a linked list using insertion sort. 题解:实现链表的插入排序. 要注意的地方就是,处理链表插入的时候尽量往当前游标的后面插入,而不要往前面插入,后者非常麻 ...
- 【输入输出挂】【Uva11462】Age Sort
例题17 年龄排序(Age Sort, UVa 11462)照从小到大的顺序输出. [输入格式] 输入包含多组测试数据.每组数据的第一行为整数n(0<n≤2 000 000),即居民总数:下一 ...
随机推荐
- spring 项目返回406
406 The resource identified by this request is only capable of generating responses with characteris ...
- Professional C# 6 and .NET Core 1.0 - Creating Hello, World! with Visual Studio
本文为转载,学习研究 Creating Hello, World! with Visual Studio Chapter 1, “.NET Application Architectures,” ex ...
- 开坑数位dp
[背景] 在10月3日的dp专练中,压轴的第6题是一道数位dp,于是各种懵逼. 为了填上这个留存已久的坑,蒟蒻chty只能开坑数位dp了. [例题一][HDU2089]不要62 题目大意:给你一个区间 ...
- mfs权威指南
1. 我在性能测试中间遇到些问题,因为我时间有限,所以希望大家一起来测试解决,群策群力.有什么问题请大家及时指出来,因为我也处在一个不断摸索的阶段. 2. mfs不多做介绍,具体细节请参考本版mfs实 ...
- js的两种查询方式 LHS and RHS
为了进一步理解,我们需要多介绍一点编译器的术语.编译器在编译过程的第二步中生成了代码,引擎执行它时,会通过查找变量 a 来判断它是否已声明过.查找的过程由作用域进行协助,但是引擎执行怎样的查找,会影响 ...
- 498B Name That Tune
传送门 题目大意 n首音乐,第i首被听出来的概率为pi,刚开始听第一首,1s后如果听出来了则放第下一首,否则接着听这一首,第i首在连续听了ti s之后一定会被听出来,问Ts后听出来的歌的期望数量. 分 ...
- PHP文件的引用
require "文件名" 或 include("文件名") 区别:若所包含文件出现错误,include()产生一个警告,require会导致程序终止
- 关于pycharm字体大小的调整
我们平常编写pyhton 可以用sublime eclipse 但是eclipse在后期需要安装很多插件,这很是麻烦,为了避免这种麻烦,我们采用pycharm来编写,但是刚装上的该软件 不建议同学们进 ...
- oracle数据库查询全系整理
oracle数据库方面的知识到今天已经整理了12篇.当然,这不是终点,这只是一个开始,希望我写的文章可以帮助更多初学数据库的童鞋快速上手,如果你觉得文章对你有帮助,那么恭喜你已经入门了,数据库里面的知 ...
- Controlling Session Behavior in Asp.Net MVC4
Posted By : Shailendra Chauhan, 06 Jan 2013 Updated On : 11 Jun 2014 By default, Asp.Net MVC support ...