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的更多相关文章

  1. 【leetcode】Insertion Sort List (middle)

    Sort a linked list using insertion sort. 思路: 用插入排序对链表排序.插入排序是指每次在一个排好序的链表中插入一个新的值. 注意:把排好序的部分和未排序的部分 ...

  2. 【链表】Insertion Sort List

    题目: Sort a linked list using insertion sort. 思路: 插入排序是一种O(n^2)复杂度的算法,基本想法相信大家都比较了解,就是每次循环找到一个元素在当前排好 ...

  3. 【HackerRank】Insertion Sort Advanced Analysis(归并排序求数列逆序数对)

    Insertion Sort is a simple sorting technique which was covered in previous challenges. Sometimes, ar ...

  4. 【LeetCode】Insertion Sort List

    Sort a linked list using insertion sort. //用到O(N)的额外空间 public class Solution { public ListNode inser ...

  5. 【LeetCode】排序 sort(共20题)

    链接:https://leetcode.com/tag/sort/ [56]Merge Intervals (2019年1月26日,谷歌tag复习) 合并区间 Input: [[1,3],[2,6], ...

  6. 【leetcode】905. Sort Array By Parity

    题目如下: 解题思路:本题和[leetcode]75. Sort Colors类似,但是没有要求在输入数组本身修改,所以难度降低了.引入一个新的数组,然后遍历输入数组,如果数组元素是是偶数,插入到新数 ...

  7. 【LeetCode OJ】Insertion Sort List

    Problem: Sort a linked list using insertion sort. The node of the linked list is defined as: /** * D ...

  8. 【leetcode刷题笔记】Insertion Sort List

    Sort a linked list using insertion sort. 题解:实现链表的插入排序. 要注意的地方就是,处理链表插入的时候尽量往当前游标的后面插入,而不要往前面插入,后者非常麻 ...

  9. 【输入输出挂】【Uva11462】Age Sort

    例题17  年龄排序(Age Sort, UVa 11462)照从小到大的顺序输出. [输入格式] 输入包含多组测试数据.每组数据的第一行为整数n(0<n≤2 000 000),即居民总数:下一 ...

随机推荐

  1. sublime添加右键菜单

    参考:https://www.zhihu.com/question/29662273/answer/45277925 @echo Off :START CLS echo *============== ...

  2. libevent源码深度剖析一

    libevent源码深度剖析一 ——序幕 张亮 1 前言 Libevent是一个轻量级的开源高性能网络库,使用者众多,研究者更甚,相关文章也不少.写这一系列文章的用意在于,一则分享心得:二则对libe ...

  3. Java 实现分页功能

    driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/test?allowMultiQueries=true&useUnic ...

  4. python pipelines 用法

    http://zacstewart.com/2014/08/05/pipelines-of-featureunions-of-pipelines.html http://blog.csdn.net/m ...

  5. 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 ...

  6. On the nightmare that is JSON Dates. Plus, JSON.NET and ASP.NET Web API

    Ints are easy. Strings are mostly easy. Dates? A nightmare. They always will be. There's different c ...

  7. Linq学习<一>

    lambda查询语法: var result =arrarylist.where(n=>n.contains("l"))  简化的委托方法实例 linq查询结构: var  ...

  8. (转)C# HTML解析示例---星星引发的血案

    原文地址:http://www.cnblogs.com/wurang/archive/2013/06/14/3119023.html [前言] 从CSDN转投cnBlog也有一段时间了,发现cnBlo ...

  9. APUE(1)----UNIX基础知识

    一.UNIX体系结构 所有操作系统都为他们所运行的程序提供服务,典型的服务包括:执行新程序.打开文件.读文件.分配存储区等.严格意义上来说,操作系统可以定义为一种软件,它控制计算机硬件资源,提供程序运 ...

  10. 【单例模式】Singleton pattern

    前言:有很多时候,在一个生命周期中我们只要一个对象就可以了,比如:线程池,缓存,对话框,日志,显卡驱动等等.如果造出多个实例,就会导致许多问题产生,例如:程序的行为异常.资源使用过量,或者说不一致的结 ...