对链表进行插入排序。

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

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

插入排序算法:

  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. 在core2.0中实现按程序集注入依赖

    前言:在Autofac的使用中,提供了个种注入的API其中GetAssemblies()用着特别的舒坦. 1.core2.0也可以使用Autofac的包,但框架自身也提供了默认的注入Api,IServ ...

  2. nginx i.com.conf

    server { listen 9090; server_name i.com; root /Users/chong/Documents/www; # Load configuration files ...

  3. Android开发 LevelListDrawable详解

    前言 此篇博客正在施工中... 作者其实就是想挖个坑备忘一下... 十分抱歉, 可以参考https://www.jianshu.com/p/f9ec65241b6b

  4. python中遇到的问题:IndentationError: unexpected indent

    在Python中写下列代码的时候,出现错误:IndentationError: unexpected indent 分析:IndentationError是缩进的错误,查看源代码发现names开始的这 ...

  5. SpringBoot_Mybatis MyBatisPlus

    一.SpringBoot中使用Mybatis springBoot中使用mybatis跟以前spring中使用方法一样. 1.mybatis配置: spring: datasource: url: j ...

  6. 19-10-29-Z

    %%%ZZYY 只是因为是Z才模一下的. ZJ一下: 考试T1写了三张纸但是它死了. T2T3暴力叕写跪了. 考试一定一定不能不严密,少推两个交点是要命的啊. 就因为叕叕少开龙龙见祖宗了. 如果考试能 ...

  7. WebLogic使用总结(二)——WebLogic卸载[转]

    一.WebLogic 12c的卸载 WebLogic的卸载是非常容易的,找到WebLogic的卸载程序,如下图所示:

  8. vue-cli 目录结构详细讲解

    https://juejin.im/post/5c3599386fb9a049db7351a8 vue-cli 目录结构详细讲解 目录 结构预览 ├─build // 保存一些webpack的初始化配 ...

  9. codeforces 1186E- Vus the Cossack and a Field

    传送门:QAQQAQ 题意:给一个01矩阵A,他的相反矩阵为B,每一次变换都会将原矩阵面积乘4成为: AB BA 矩阵的左上角固定,变换无限次,现有q个询问,即求一个矩阵内的1的个数. 思路:因为反转 ...

  10. 2019牛客暑期多校赛(第三场)B-求01串中的最长01数量相等的子串和子序列

    https://ac.nowcoder.com/acm/contest/883/B 首先先把0所在的位置变-1,1所在位置变1,然后统计一个前缀和,用sum[i]表示. 那么如果从起点开始的话只要满足 ...