对链表进行插入排序。

从第一个元素开始,该链表可以被认为已经部分排序(用黑色表示)。

每次迭代时,从输入数据中移除一个元素(用红色表示),并原地将其插入到已排好序的链表中。

插入排序算法:

  1. 插入排序是迭代的,每次只移动一个元素,直到所有元素可以形成一个有序的输出列表。
  2. 每次迭代中,插入排序只从输入数据中移除一个待排序的元素,找到它在序列中适当的位置,并将其插入。
  3. 重复直到所有输入数据插入完为止。

示例 1:

输入: 4->2->1->3 输出: 1->2->3->4

示例 2:

输入: -1->5->3->4->0 输出: -1->0->3->4->5

class Solution {
public:
ListNode* insertionSortList(ListNode* head)
{
if(head == NULL)
return NULL;
ListNode *node = head ->next;
ListNode *newhead = new ListNode(0);
newhead ->next = head;
head ->next = NULL;
while(node != NULL)
{
ListNode *last = newhead;
ListNode *cur = newhead ->next;
ListNode *nextNode = node ->next;
bool isInsert = false;
while(cur != NULL)
{
if(node ->val > cur ->val)
{
last = cur;
cur = cur ->next;
}
else
{
isInsert = true;
last ->next = node;
node ->next = cur;
break;
}
}
if(!isInsert)
{
last ->next = node;
node ->next = NULL;
}
node = nextNode;
if(nextNode != NULL)
nextNode = nextNode ->next;
}
return newhead ->next;
}
};

Leetcode147. Insertion Sort List对链表进行插入排序的更多相关文章

  1. leetcode——Insertion Sort List 对链表进行插入排序(AC)

    Sort a linked list using insertion sort. class Solution { public: ListNode *insertionSortList(ListNo ...

  2. 9. Sort List && Insertion Sort List (链表排序总结)

    Sort List Sort a linked list in O(n log n) time using constant space complexity.                   H ...

  3. LeetCode147:Insertion Sort List

    题目: Sort a linked list using insertion sort. 解题思路: 按题目要求,直接进行插入排序 实现代码: #include <iostream> us ...

  4. The insertion sort algorithm expressed in pseudocode - 插入排序

    Computer Science An Overview _J. Glenn Brookshear _11th Edition procedure Sort (List) N ← 2; while ( ...

  5. LeetCode(147) Insertion Sort List

    题目 Sort a linked list using insertion sort. 分析 实现链表的插入排序 注意: 程序入口的特殊输入判断处理! 节点的链接处理,避免出现断链! AC代码 /** ...

  6. [Swift]LeetCode147. 对链表进行插入排序 | Insertion Sort List

    Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...

  7. [LeetCode] Insertion Sort List 链表插入排序

    Sort a linked list using insertion sort. 链表的插入排序实现原理很简单,就是一个元素一个元素的从原链表中取出来,然后按顺序插入到新链表中,时间复杂度为O(n2) ...

  8. insertion sort list (使用插入排序给链表排序)

    Sort a linked list using insertion sort. 对于数组的插入排序,可以参看排序算法入门之插入排序(java实现),遍历每个元素,然后相当于把每个元素插入到前面已经排 ...

  9. Insertion Sort List——链表的插入排序

    Sort a linked list using insertion sort. 这道题跟 Sort List 类似,要求在链表上实现一种排序算法,这道题是指定实现插入排序.插入排序是一种O(n^2) ...

随机推荐

  1. Window中在Intellij idea开发时常用快捷键

    以下idea中的快捷键是在window 7中确认过,如果快捷键不起作用,可能是该快捷键被其它软件占用,或系统不同导致. 1.Ctrl + Z:撤回代码: 2.Ctrl + Shift + Z:恢复撤回 ...

  2. POJ - 3294~Relevant Phrases of Annihilation SPOJ - PHRASES~Substrings POJ - 1226~POJ - 3450 ~ POJ - 3080 (后缀数组求解多个串的公共字串问题)

    多个字符串的相关问题 这类问题的一个常用做法是,先将所有的字符串连接起来, 然后求后缀数组 和 height 数组,再利用 height 数组进行求解. 这中间可能需要二分答案. POJ - 3294 ...

  3. sparkStreaming入门

    1.环境 jdk : 1.8 scala : 2.11.7 hadoop:2.7 spark : 2.2.0 2. 开发工具 idea 2017.2 3.maven的pom文件 <depende ...

  4. eclipse 克隆 https 地址的 Git 仓库报错:cannot open git-upload-pack

    解决方法:Window >Preferences >Team>Git>User settings点击Add Entry设置key:http.sslVerify value:fa ...

  5. 【学术篇】luogu2778 [AHOI2016初中组]迷宫(代码高能!)

    好久好久我都没有刷题了. 题目の传送门:https://www.luogu.org/problem/show?pid=2778 题目大意:(啥 题目讲得不够清楚?)平面内有n个以整点(就是坐标都是整数 ...

  6. 快速沃尔什变换(FWT) 与 快速莫比乌斯变换 与 快速沃尔什变换公式推导

    后面的图片将会告诉: 如何推出FWT的公式tf 如何推出FWT的逆公式utf 用的是设系数,求系数的方法! ============================================== ...

  7. 常用DOM API总结

    一. 获取节点 1. 获取元素节点 getElementsById getElementsByTagName getElementsByClassName 2. 获取属性节点 getAttribute ...

  8. wget: command not found 解决方案

    wget: command not found 解决方案 wget command not found 解决方案 问题分析 解决方案 方法一yum安装wget 方法二rpm安装 问题分析 安装的是Ce ...

  9. NSLayoutConstraint-代码实现自动布局的函数用法说明

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://rainbownight.blog.51cto.com/1336585/13161 ...

  10. 使用 data-* 属性来嵌入自定义数据:

    <!DOCTYPE html> <html> <head> <script> function showDetails(animal) { var an ...