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

Example 1:

Input: 4->2->1->3
Output: 1->2->3->4

Example 2:

Input: -1->5->3->4->0
Output: -1->0->3->4->5 Solution:
  复杂度为O(nlogn)有快速排序,并归排序,堆排序,对于来列表而言,堆排序最适合了
  使用快慢指针将链表分为两部分
  merge01为简洁版
 class Solution {
public:
ListNode* sortList(ListNode* head) {
if (head == nullptr || head->next == nullptr)return head;
ListNode *slow = head, *fast = head, *pre = head;
while (fast != nullptr && fast->next != nullptr)
{
pre = slow;
slow = slow->next;
fast = fast->next->next;
}
pre->next = nullptr;//将链表分为两段
return merge(sortList(head), sortList(slow));
}
ListNode* merge01(ListNode* l1, ListNode* l2)
{
if (l1 == nullptr || l2 == nullptr)return l1 == nullptr ? l2 : l1;
if (l1->val < l2->val)
{
l1->next = merge(l1->next, l2);
return l1;
}
else
{
l2->next = merge(l1, l2->next);
return l2;
}
}
ListNode* merge02(ListNode* l1, ListNode* l2)
{
ListNode* ptr = new ListNode(-);
ListNode* p = ptr;
while (l1 != nullptr && l2 != nullptr)
{
if (l1->val < l2->val)
{
p->next = l1;
l1 = l1->next;
}
else
{
p->next = l2;
l2 = l2->next;
}
p = p->next;
}
if (l1 == nullptr)p->next = l2;
if (l2 == nullptr)p->next = l1;
return ptr->next;
}
};

力扣算法题—148sort-list的更多相关文章

  1. 力扣算法题—069x的平方根

    实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去. 示例 1: 输入: 4 输出: 2 示例 ...

  2. 力扣算法题—060第K个排列

    给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: "123" "132&qu ...

  3. 力扣算法题—050计算pow(x, n)

    #include "000库函数.h" //使用折半算法 牛逼算法 class Solution { public: double myPow(double x, int n) { ...

  4. 力扣算法题—147Insertion_Sort_List

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

  5. 力扣算法题—093复原IP地址

    给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式. 示例: 输入: "25525511135" 输出: ["255.255.11.135", ...

  6. 力扣算法题—079单词搜索【DFS】

    给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字母不允许被重复使用. ...

  7. 力扣算法题—052N皇后问题2

    跟前面的N皇后问题没区别,还更简单 #include "000库函数.h" //使用回溯法 class Solution { public: int totalNQueens(in ...

  8. 力扣算法题—051N皇后问题

    #include "000库函数.h" //使用回溯法来计算 //经典解法为回溯递归,一层一层的向下扫描,需要用到一个pos数组, //其中pos[i]表示第i行皇后的位置,初始化 ...

  9. 力扣算法题—143ReorderList

    Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You may not mod ...

  10. 力扣算法题—144Binary Tree Preorder Traversal

    Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1,null,2,3 ...

随机推荐

  1. php_openssl.dll' - 找不到指定的模块。 in Unknown on line 0

    今天在windows2003配置IIS+php_openssl,php-error.log老是提示“PHPWarning:PHPStartup:Unabletoloaddynamiclibrary'C ...

  2. l1和l2正则化

    https://blog.csdn.net/tianguiyuyu/article/details/80438630 以上是莫烦对L1和L2的理解 l2正则:权重的平方和,也就是一个圆 l1正则:权重 ...

  3. list中的所有值转换为字符串,以及list拼接成一个字符串

    import stringlis=[1,2,3,'abc']fw=open('hello.txt','w',encoding='utf-8')# print(''.join(str(lis).repl ...

  4. 重定向和转向的写法,重定向以post方式提交

    重转向保留跳转过来的Referer,路径不会变1 request.getRequestDispatcher("/eventweb/index.sp?loginId=" + logi ...

  5. 解决“每次打开office2010的word都会出现配置进度框”问题

       在win7中安装完office2010后.打开 *.doc文件时,总会弹出"配置进度框"问题,解决例如以下:  1)点击"開始"-->"执 ...

  6. casperjs-options

    The Casper class The easiest way to get a casper instance is to use the module's create() method: 最简 ...

  7. js非布尔值的与(&&)与或(||)运算

    /** * 非布尔值的与(&&)与或(||)运算 * 1.先将其转换成布尔值再做运算,并且返回原值 * 2.与(&&)运算: * a.如果第一个值为true,则返回第二 ...

  8. 选择器与过滤器(全)————JQ

    JQ基础--选择器与过滤器(全) JQ选择器 <!DOCTYPE html> <html> <head> <meta charset="UTF-8& ...

  9. 如何解决“ VMware Workstation 不可恢复错误: (vcpu-0) vcpu-0:VERIFY vmcore/vmm/main/cpuid.c:386 bugNr=1036521”

    第一次装虚拟机,装centos7遇到的坑: 1. 出现 “VMware Workstation 不可恢复错误: (vcpu-0) vcpu-0:VERIFY vmcore/vmm/main/cpuid ...

  10. mybatis 自定义查询语句

    通过mybatis插件生成的mapper文件只有基本的增.删.改.查.汇总.但是实际使用场景中,总是有各种需要连表.汇总.分组查询的需求,那我们一般都通过自定义查询语句去实现. 有时候会有表结构更改的 ...