对链表进行插入排序,比对数组排序麻烦一点。

ListNode *insertSortList(ListNode *head)
{
ListNode dummy(-);
for (ListNode *cur = head; cur != nullptr;)
{
//将当前结点插入到此结点之后
auto Pos = findPos(&dummy, cur->val);
//保存当前结点的下一个结点
ListNode *temp = cur->next;
//插入
cur->next = Pos->next;
Pos->next = cur;
//继续下一个结点
cur = temp;
}
} ListNode *findPos(ListNode *head, int val)
{
ListNode *pre = nullptr;
for (ListNode *cur = head; cur != nullptr&&cur->val <= val; pre = cur, cur = cur->next); return pre;
}

Insertion Sort List的更多相关文章

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

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

  2. 经典排序算法 – 插入排序Insertion sort

    经典排序算法 – 插入排序Insertion sort  插入排序就是每一步都将一个待排数据按其大小插入到已经排序的数据中的适当位置,直到全部插入完毕. 插入排序方法分直接插入排序和折半插入排序两种, ...

  3. leetcode Insertion Sort List

    题目:Sort a linked list using insertion sort. 代码: /** * Definition for singly-linked list. * struct Li ...

  4. 【leetcode】Insertion Sort List (middle)

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

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

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

  6. LeetCode OJ 147. Insertion Sort List

    Sort a linked list using insertion sort. Subscribe to see which companies asked this question 解答 对于链 ...

  7. Java for LeetCode 147 Insertion Sort List

    Sort a linked list using insertion sort. 解题思路: 插入排序,JAVA实现如下: public ListNode insertionSortList(List ...

  8. 【LeetCode OJ】Insertion Sort List

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

  9. 147. Insertion Sort List

    Sort a linked list using insertion sort. 代码如下: /** * Definition for singly-linked list. * public cla ...

  10. leetcode 147. Insertion Sort List ----- java

    Sort a linked list using insertion sort. 插入排序. /** * Definition for singly-linked list. * public cla ...

随机推荐

  1. 论Visual Studio和.NET Framework

    今天在工作的时候听到一席谈话感觉有点不可思议,微软真的是把开发人员惯的有点傻了,微软流水线式的产品让很多开发者认定了"唯一",这当然也说明了微软的成功,不扯太多题外话,今天只是简单 ...

  2. css的6中居中的方式

    请先看blog:http://blog.csdn.net/wolinxuebin/article/details/7615098

  3. 第四章:javascript: 栈

    列表是一种最自然的数据组织方式.上一章已经介绍如何使用List类将数据组织成一个列表.如果数据存储的顺序不重要.也不必对数据进行查找,那么列表就是一种再好不过的数据结构.对于其它的一些应用,列表就显得 ...

  4. shiro 与 web 的结合

    本次使用的jar包为 shiro-core-.jar shiro-web-.jar 从Shiro 1.2开始引入了Environment/WebEnvironment的概念,即由它们的实现提供相应的S ...

  5. TinyMCE(富文本编辑器)

    [转]TinyMCE(富文本编辑器)在Asp.Net中的使用方法 官网演示以及示例代码:https://www.tinymce.com/docs/demo/image-tools/ 转自:http:/ ...

  6. 【UESTC 482】Charitable Exchange(优先队列+bfs)

    给你n个物品交换,每个交换用r,v,t描述,代表需要用r元的东西花费t时间交换得v元的东西.一开始只有1元的东西,让你求出交换到价值至少为m的最少时间代价.相当于每个交换是一条边,时间为边权,求走到价 ...

  7. codeforces 720A:Closing ceremony

    Description The closing ceremony of Squanch Code Cup is held in the big hall with n × m seats, arran ...

  8. Erlang第二课 ---- bit串

    Erlang是被设计来用在电信设备中的,这意味着需要处理大量的二进制数据.也正因为如此,Erlang把binary和binary string提升到了一个相当高的位置,提供了极为丰富的操作机制.当然, ...

  9. DLUTOJ #1394 Magic Questions

    传送门 Time Limit: 3 Sec  Memory Limit: 128 MB Description Alice likes playing games. So she will take ...

  10. jquery------脚注的使用

    index.jsp <script type="text/javascript" src="../js/my.js"></script> ...