当前位置: 首页 > news >正文

Leetcode 148. 排序链表 归并排序

原题链接:Leetcode 148. 排序链表

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

方法一:自顶向下归并排序

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode() : val(0), next(nullptr) {}*     ListNode(int x) : val(x), next(nullptr) {}*     ListNode(int x, ListNode *next) : val(x), next(next) {}* };*/
class Solution {
public:ListNode* merge2Lists(ListNode* head1,ListNode* head2){if(!head1) return head2;if(!head2) return head1;if(head1-> val <=  head2->val){head1->next = merge2Lists(head1->next,head2);return head1;}else {head2->next = merge2Lists(head1,head2->next);return head2;}}ListNode* sortList(ListNode* head) {if(head==nullptr || head->next==nullptr) return head;// 快慢指针找到中间节点ListNode * slow = head;ListNode * fast = head;ListNode * pre = slow;while(fast && fast->next){pre = slow;slow = slow->next;fast = fast->next->next;}// 找到中间节点 slow,断开slow的前一个节点pre和slow的关系,将链表拆分成两个子链表pre ->next = nullptr;// 对两个子链表分别排序head = sortList(head);slow = sortList(slow);// 合并两个有序链表return merge2Lists(head,slow);}
};

方法二:自底向上归并排序

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode() : val(0), next(nullptr) {}*     ListNode(int x) : val(x), next(nullptr) {}*     ListNode(int x, ListNode *next) : val(x), next(next) {}* };*/
class Solution {
public:ListNode* merge2Lists(ListNode* head1,ListNode* head2){if(!head1) return head2;if(!head2) return head1;if(head1-> val <=  head2->val){head1->next = merge2Lists(head1->next,head2);return head1;}else {head2->next = merge2Lists(head1,head2->next);return head2;}}ListNode* sortList(ListNode* head) {if(head==nullptr || head->next==nullptr) return head;int n=0;ListNode* cur = head;while(cur){cur=cur->next;n++;}ListNode* root = new ListNode (0,head);for(int len=1;len<n;len*=2){ListNode* pre = root;cur = root->next;while(cur!=nullptr){ListNode* head1 = cur;for(int i=1;i<len && cur->next!=nullptr;i++){cur = cur->next;}ListNode* head2 = cur->next;cur->next = nullptr;cur= head2;for(int i=1;i<len && cur!=nullptr && cur->next!=nullptr ;i++){cur = cur->next;}ListNode* nxt = nullptr;if(cur!=nullptr){nxt = cur->next;cur->next = nullptr;}ListNode* merged = merge2Lists(head1,head2);pre->next=merged;while(pre->next!=nullptr){pre = pre->next;}cur = nxt;}}return root->next;}
};
http://www.dtcms.com/a/406642.html

相关文章:

  • 手机参数对比的网站宁波网站推广方式
  • 智能边缘:下一代边缘计算架构设计与实践
  • 电力专用通讯管理机,搭载国产化CPU,提供多网多串等丰富的通讯接口,适配麒麟、凝思等国 产化操作系统
  • 网站建设的岗位是什么岗位深圳公司注册的服务机构
  • 栈的进阶篇
  • 通义千问大模型调用
  • KMP和扩展KMP
  • 做房产信息互联网网站需要什么资质织梦开发供需网站
  • 汽车安全性能测试与铸铁底座的重要性
  • 郴州网站建设解决方案计算机网页制作工具
  • DHCP服务器
  • wordpress nginx 固定链接seo推广的特点
  • AI算力加速
  • 《强化学习数学原理》学习笔记1——贝尔曼期望公式推导
  • 9.9元奶茶项目:matlab+FPGA的cordic算法(双曲线向量模式)计算对数log
  • 发布网站需要备案安徽省住房城乡建设厅官方网站
  • 初识MYSQL —— 数据库基础
  • PHP 8.5 升级指南 了解即将废弃的 11 个功能和完整迁移方案
  • 月饼杯II
  • OpenHarmony位置服务模块架构设计
  • 河南制作网站国外wordpress主题
  • C# WPF DataGrid 数据绑定时的单元格编辑类型模板
  • 天津微网站做年报的网站
  • Marin说PCB之POC电路layout设计仿真案例---13
  • 随堂笔记2025-925
  • 龙岩建网站wordpress php7 500
  • 阿里云 iis 多个网站建筑公司资质
  • Python读取MongoDB的JSON字典和列表对象转为字符串
  • 论证框架的四个核心问题(概念界定/基础理论/研究模型/研究假设)的因果链
  • IO流与网络编程