排序链表——力扣148

文章目录

      • 题目描述
      • 法一 自顶向下归并排序
      • 法二)自底向上归并排序

题目描述

在这里插入图片描述
在这里插入图片描述

题目的进阶问题要求达到 O(nlogn) 的时间复杂度和 O(1) 的空间复杂度,时间复杂度是 O(nlogn) 的排序算法包括归并排序、堆排序和快速排序(快速排序的最差时间复杂度是 O(n2),其中最适合链表的排序算法是归并排序
在这里插入图片描述

法一 自顶向下归并排序

在这里插入图片描述

class Solution {
public:ListNode* sortList(ListNode* head){return sortList(head, nullptr);}ListNode* sortList(ListNode* head, ListNode* tail){if(head==nullptr){return head;}if(head->next==tail){head->next=nullptr;return head;}ListNode *fast=head, *slow = head;while(fast!=tail){slow = slow->next;fast = fast->next;if(fast!=tail){fast = fast->next;}}ListNode* mid = slow;return merge(sortList(head, mid), sortList(mid, tail));}ListNode* merge(ListNode *l1, ListNode *l2){ListNode *dummy = new ListNode(-1);ListNode *cur=dummy, *s1=l1, *s2=l2;while(s1 && s2){if(s1->val < s2->val){cur->next = s1;s1 = s1->next;} else {cur->next = s2;s2 = s2->next;}cur = cur->next;}cur->next = s1 ? s1 : s2;return dummy->next; }
};

在这里插入图片描述

法二)自底向上归并排序

在这里插入图片描述
在这里插入图片描述

class Solution {
public:ListNode* sortList(ListNode* head) {if (head == nullptr) {return head;}int length = 0;ListNode* node = head;while (node != nullptr) {length++;node = node->next;}ListNode* dummyHead = new ListNode(0, head);for (int subLength = 1; subLength < length; subLength <<= 1) {ListNode* prev = dummyHead, *curr = dummyHead->next;while (curr != nullptr) {ListNode* head1 = curr;for (int i = 1; i < subLength && curr->next != nullptr; i++) {curr = curr->next;}ListNode* head2 = curr->next;curr->next = nullptr;curr = head2;for (int i = 1; i < subLength && curr != nullptr && curr->next != nullptr; i++) {curr = curr->next;}ListNode* next = nullptr;if (curr != nullptr) {next = curr->next;curr->next = nullptr;}ListNode* merged = merge(head1, head2);prev->next = merged;while (prev->next != nullptr) {prev = prev->next;}curr = next;}}return dummyHead->next;}ListNode* merge(ListNode* head1, ListNode* head2) {ListNode* dummyHead = new ListNode(0);ListNode* temp = dummyHead, *temp1 = head1, *temp2 = head2;while (temp1 != nullptr && temp2 != nullptr) {if (temp1->val <= temp2->val) {temp->next = temp1;temp1 = temp1->next;} else {temp->next = temp2;temp2 = temp2->next;}temp = temp->next;}if (temp1 != nullptr) {temp->next = temp1;} else if (temp2 != nullptr) {temp->next = temp2;}return dummyHead->next;}
};

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/13046.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【C#】.Net Framework框架下的Authorize权限类

2023年&#xff0c;第31周&#xff0c;第3篇文章。给自己一个目标&#xff0c;然后坚持总会有收货&#xff0c;不信你试试&#xff01; 在C#的.NET Framework中&#xff0c;你可以使用Authorize类来处理权限认证。Authorize类位于System.Web.Mvc命名空间中&#xff0c;它提供了…

关于Docker的知识点

Docker是一个快速交付应用、运行应用的技术。 Docker基本操作--容器 示例&#xff1a;创建运行一个Nginx容器

Python不是一门伟大的语言

作为一门简洁易用、生态蓬勃且具有高泛用性的编程语言&#xff0c;Python一直以来都被不少人称作“编程语言中的瑞士军刀”。 尤其随着近来AI热潮席卷全球&#xff0c;Python在编程语言圈中的地位也随之水涨船高&#xff0c;甚至一度被视作AI专用语言或大数据专用语言。 然而…

QT第四讲

思维导图 基于QT的网络聊天室 widget.h #ifndef WIDGET_H #define WIDGET_H#include <QWidget> #include<QTcpServer> //服务器类 #include<QTcpSocket> //客户端类 #include<QMessageBox> //对话框类 #include<QList…

css之文字描边

文章目录 传统方式实现第二种方式 传统方式实现 html <p>文字描边效果</p>scss mixin text-stroke($color: #ffffff, $width: 1px) {text-shadow: 0 -#{$width} #{$color},#{$width} -#{$width} #{$color},#{$width} 0 #{$color},#{$width} #{$width} #{$color}…

【AI】roop给视频换脸记录

环境 安装Anaconda3基于Anaconda3创建一个python3.10的环境安装Git安装ffmpeg 我的操作系统版本是Ubuntu 18.04.4 LTS&#xff08;带GPU会快一点&#xff09;&#xff0c;以上所说的 环境请自行百度安装&#xff01;快速使用 拉取项目 git clone gitgithub.com:VoidAndNull…

Spring Cache

什么是Spring Cache&#xff1f; Spring Cache是Spring框架的一个模块&#xff0c;它提供了对应用程序方法级别的缓存支持。通过使用Spring Cache&#xff0c;您可以在方法的结果被计算后&#xff0c;将其缓存起来&#xff0c;从而避免相同输入导致的重复计算。 Spring Cache…

AWS考试认证学习(二)

一、AWS高级 SAP-C01考试 AWS高级考试 AWS Certified Solutions Architect - Professional 报名费&#xff1a;300美金 SAP-C01考试内容主要覆盖的五大领域和对应领域所占权重&#xff1a; 1、组织复杂性设计 12.5% 2、新解决方案设计 31% 3、迁移规划 15% 4、成本控制…

Linux 发行版 CentOS 于 Ubuntu 软件的安装、卸载、查找

CentOS于Ubuntu 内核都是Linux&#xff0c;是一样的。 CentOS 软件格式 .rpm sudo yum [-y] [ install | remove | search ] 软件名称 install 安装 remove 移除 search 搜索 Ubuntu 软件格式 .deb sudo apt [-y] [ install | remove | search ] 软件名称 install 安装 remove…

二叉树的层序遍历(两种方法:迭代+递归)

题目&#xff1a; 给你二叉树的根节点 root &#xff0c;返回其节点值的 层序遍历 。 &#xff08;即逐层地&#xff0c;从左到右访问所有节点&#xff09;。 输入&#xff1a;root [3,9,20,null,null,15,7] 输出&#xff1a;[[3],[9,20],[15,7]] 解题思路&#xff1a;迭代法…

Vector<T> 动态数组(随机访问迭代器)(答案)

答案如下 //------下面的代码是用来测试你的代码有没有问题的辅助代码,你无需关注------ #include <algorithm> #include <cstdlib> #include <iostream> #include <vector> #include <utility> using namespace std; struct Record { Record…

第一财经专访张晨:图技术赋能金融领域进入全新阶段

2023世界人工智能大会期间&#xff0c;创邻科技创始人兼CEO张晨博士受邀走进第一财经的演播室&#xff0c;围绕人工智能技术和图技术在金融行业的赋能展开讨论&#xff0c;为线上数万名观众分享精彩前沿观点。 现场直击 AI赋能金融 大模型催生新业态 | 图技术赋能金融领域进入…

string类的模拟实现

文章目录 string类的模拟实现string基本框架的实现operator的实现string常用函数的实现 string类的模拟实现 前文对于string的常用函数做了讲解&#xff0c;由于string是一个面试官常考的点&#xff0c;总喜欢让模拟实现string类&#xff0c;下面来模拟实现一下string&#xf…

优化企业集成架构:iPaaS集成平台助力数字化转型

前言 在数字化时代全面来临之际&#xff0c;企业正面临着前所未有的挑战与机遇。技术的迅猛发展与数字化转型正在彻底颠覆各行各业的格局&#xff0c;不断推动着企业迈向新的前程。然而&#xff0c;这一数字化时代亦衍生出一系列复杂而深奥的难题&#xff1a;各异系统之间数据…

Vite创建Vue+TS项目引入文件路径报错

使用vite搭建vue3脚手架的时候&#xff0c;发现main.ts中引入App.vue编辑器会报错&#xff0c;但是不影响代码运行。 报错信息&#xff1a;TS2307: Cannot find module ‘./App.vue’ or its corresponding type declarations. 翻译过来是找不到模块或者相关的声明类型&#…

如何快速模拟一个后端 API

第一步&#xff1a;创建一个文件夹&#xff0c;用来存储你的数据 数据&#xff1a; {"todos": [{ "id": 1, "text": "学习html44", "done": false },{ "id": 2, "text": "学习css", "…

Redis7学习笔记01

一、redis7实战教程简洁 1、大纲&#xff1a; ①、适合对象&#xff0c;从小白到熟手&#xff0c;一套全包圆 ②、Redis专题-大厂面试题&#xff0c;含100道 ③、Redis专题-真实需求生产真实案例 ④、Redis7新特性 2、小白篇高阶篇&#xff1a; 3、大厂面试题&#xff1a…

自动化测试——APP测试

一、环境配置 1、安装jdk 配置环境变量 2、Android SDK 环境安装 3、Appium Server安装 4、模拟器安装 5、安装appium-python-client Python第三方库 二、APP自动化测试原理 三、Desired Capabilites——APPium自动化配置项 1、设置参数 2、操作系统 3、选择版本 4、设备名称…

唯一索引异常捕获aop切面工具类

定义自定义唯一索引捕获注解 /*** author huaqiang* version 1.0* description 自定义唯一索引异常* date 2023/7/28*/ Target({ElementType.METHOD, ElementType.TYPE}) Retention(RetentionPolicy.RUNTIME) public interface CustomDuplicateKeyException {String[] keyValu…

TCP KeepAlive与HTTP Keep-Alive

TCP KeepAlive与HTTP Keep-Alive TCP KeepAliveHTTP Keep-AliveTCP服务器怎么检测客户端断开连接 TCP KeepAlive TCP连接建立之后&#xff0c;如果应用程序或者上层协议一直不发送数据&#xff0c;或者隔很长时间才发送一次数据&#xff0c;那么TCP需要判断是应用程序掉线了还…