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

23. Merge k Sorted Lists

目录

题目描述

方法一、k-1次两两合并

方法二、分治法合并

方法三、使用优先队列


题目描述

23. Merge k Sorted Lists

方法一、k-1次两两合并

选第一个链表作为结果链表,每次将后面未合并的链表合并到结果链表中,经过k-1次合并,即可得到答案。假设每个链表的最长长度是n,时间复杂度O(n+2n+3n+...(k-1)n) = O(\frac{k(k-1))}{2}n) = O(k^{2}n)。空间复杂度O(1)。

/*** 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* mergeKLists(vector<ListNode*>& lists) {int n = lists.size();if(n == 0)return nullptr;ListNode* ans = lists[0];for(int i = 1;i< n;i++){ans = merge(ans,lists[i]);}return ans;}ListNode* merge(ListNode* L1,ListNode* L2){ListNode* dummy = new ListNode();ListNode* cur = dummy;while(L1&&L2){if(L1->val < L2->val){cur->next = L1;cur = L1;L1 = L1->next;}else{cur->next = L2;cur = L2;L2 = L2->next;}}cur->next = L1 != nullptr ? L1 : L2;ListNode* res = dummy->next;delete dummy;return res;}
};

方法二、分治法合并

时间复杂度 O(kn×logk)。空间复杂度 O(logk) 。

/*** 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* mergeKLists(vector<ListNode*>& lists) {int n = lists.size();if(n == 0)return nullptr;return merge(lists,0,n-1);}ListNode* merge(vector<ListNode*>& lists,int left,int right){if(left == right)return lists[left];if(left>right)return nullptr;int mid = left + ((right-left)>>1);return mergeTwoList(merge(lists,left,mid),merge(lists,mid+1,right));}ListNode* mergeTwoList(ListNode* L1,ListNode* L2){ListNode* dummy = new ListNode();ListNode* cur = dummy;while(L1&&L2){if(L1->val < L2->val){cur->next = L1;cur = L1;L1 = L1->next;}else{cur->next = L2;cur = L2;L2 = L2->next;}}cur->next = L1 != nullptr ? L1 : L2;ListNode* res = dummy->next;delete dummy;return res;}
};

方法三、使用优先队列

时间复杂度 O(kn×logk)。空间复杂度 O(k) 。

/*** 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 {struct Node{ListNode* node_ptr;int val;bool operator<(const Node& rhs) const{return val>rhs.val;}};
public:ListNode* mergeKLists(vector<ListNode*>& lists) {priority_queue<Node> Heap;for(auto& node:lists){if(node){Heap.push({node,node->val});}}ListNode* head = nullptr;ListNode* cur = nullptr;while(!Heap.empty()){if(head == nullptr){head = Heap.top().node_ptr;cur = head;}else{cur->next = Heap.top().node_ptr;cur = cur->next;}Heap.pop();if(cur->next){Heap.push({cur->next,cur->next->val});}}return head;}
};
http://www.xdnf.cn/news/10630.html

相关文章:

  • Alist Win 基本用法
  • JavaSE知识总结(集合篇) ~个人笔记以及不断思考~持续更新
  • Python中使用pandas
  • C++ list代码练习、set基础概念、set对象创建、set大小操作
  • SQL 窗口函数深度解析:ROW_NUMBER 实战指南
  • volatile,synchronized,原子操作实现原理,缓存一致性协议
  • LabVIEW准分子激光器智能控制系统
  • 35.x64汇编写法(二)
  • Elasticsearch 读写流程深度解析
  • JAVA中的注解和泛型
  • 用 Whisper 打破沉默:AI 语音技术如何重塑无障碍沟通方式?
  • Mybatis框架各配置文件主要内容详解(二)
  • 神经网络与深度学习(第二章)
  • 数字化转型全场景安全解析:从产品到管理的防线构建与实施要点
  • 由浅入深一文详解同余原理
  • 【Android】MT6835 + MT6631 WiFi进入Meta模式出现WiFi_HQA_OpenAdapter failed
  • Higress项目解析(二):Proxy-Wasm Go SDK
  • 车载诊断架构 --- DTC消抖参数(Trip Counter DTCConfirmLimit )
  • 12.1 GUI 事件处理
  • nssctf第二题[SWPUCTF 2021 新生赛]简简单单的逻辑
  • BiliNote部署实践
  • CRC 原理概述
  • NodeJS全栈WEB3面试题——P5全栈集成与 DApp 构建
  • 04powerbi-度量值-筛选引擎CALCULATE()
  • HTTP、WebSocket、SSE 对比
  • hadoop伪分布式配置(单机)
  • 迈向分布式智能:解析MCP到A2A的通信范式迁移
  • 大数据学习(127)-hive日期函数
  • ACTF2025-web-eznote-wp
  • 详解一下RabbitMQ中的channel.Publish