21.Merge Two Sorted Lists 、23. Merge k Sorted Lists
21.Merge Two Sorted Lists
初始化一个指针作为开头,然后返回这个指针的next
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode* dummy = new ListNode(-);
ListNode* p = dummy;
while(l1 && l2){
if(l1->val <= l2->val){
p->next = l1;
p = p->next;
l1 = l1->next;
}
else{
p->next = l2;
p = p->next;
l2 = l2->next;
}
}
if(l1)
p->next = l1;
else
p->next = l2;
return dummy->next;
}
};
23. Merge k Sorted Lists
https://www.cnblogs.com/grandyang/p/4606710.html
这个是分治的思想
实质上就是每次合并一半的链表,且两两合并的链表按照一定间隔距离进行合并
class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {
int n = lists.size();
if(n <= )
return NULL;
while(n > ){
int k = (n + )/;
for(int i = ;i < n/;i++)
lists[i] = mergeList(lists[i],lists[i + k]);
n = k;
}
return lists[];
}
ListNode* mergeList(ListNode* l1,ListNode* l2){
if(l1 == NULL)
return l2;
if(l2 == NULL)
return l1;
ListNode* head;
if(l1->val < l2->val){
head = l1;
head->next = mergeList(l1->next,l2);
}
else{
head = l2;
head->next = mergeList(l1,l2->next);
}
return head;
}
};
自己写的:
用非递归也可以合并两个链表。
k = (n + 1)/2中k代表间隔,vector中的链表等间隔合并,这样能达到减少一半的目的。+1的目的是针对奇数这种情况,中间一定会剩下一个单独的,这个单独的也要保留在vector中。n代表当前已更新剩下的链表个数,其实也就是存放在lists中的前n个。+1的目的其实也是针对奇数个的情况。
class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {
if(lists.empty())
return NULL;
int n = lists.size();
while(n > ){
int k = (n + )/;
for(int i = ;i < n/;i++)
lists[i] = merge(lists[i],lists[i + k]);
n = (n + )/;
}
return lists[];
}
ListNode* merge(ListNode* l1,ListNode* l2){
ListNode* dummy = new ListNode(-);
ListNode* p = dummy;
while(l1 && l2){
if(l1->val < l2->val){
p->next = l1;
p = p->next;
l1 = l1->next;
}
else{
p->next = l2;
p = p->next;
l2 = l2->next;
}
}
if(l1)
p->next = l1;
else
p->next = l2;
return dummy->next;
}
};
21.Merge Two Sorted Lists 、23. Merge k Sorted Lists的更多相关文章
- 刷题23. Merge k Sorted Lists
一.题目说明 这个题目是23. Merge k Sorted Lists,归并k个有序列表生成一个列表.难度为Hard,实际上并不难,我一次提交就对了. 二.我的解答 就是k路归并,思路很简单,实现也 ...
- LeetCode 23. 合并K个排序链表(Merge Two Sorted Lists)
23. 合并K个排序链表 23. Merge k Sorted Lists 题目描述 合并 k 个排序链表,返回合并后的排序链表.请分析和描述算法的复杂度. LeetCode23. Merge k S ...
- [Leetcode][Python]23: Merge k Sorted Lists
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 23: Merge k Sorted Listshttps://oj.leet ...
- LeetCode LinkList 23. Merge k Sorted Lists
这两天一直也没有顾上记录一下自己做过的题目,回头看看,感觉忘的好快,今天做了一个hard,刚开始觉得挺难得,想了两种方法,一种是每次都从k个list中选取最小的一个,为空的直接跳过,再就是每次合并其中 ...
- [LeetCode] 23. Merge k Sorted Lists 合并k个有序链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. E ...
- 【LeetCode】23. Merge k Sorted Lists 合并K个升序链表
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,链表,单链表,题解,leetcode, 力扣,Py ...
- 23. Merge k Sorted Lists - LeetCode
Question 23. Merge k Sorted Lists Solution 题目大意:合并链表数组(每个链表中的元素是有序的),要求合并后的链表也是有序的 思路:遍历链表数组,每次取最小节点 ...
- [leetcode 23]Merge k Sorted Lists
1 题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexi ...
- 23. Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. = ...
随机推荐
- Java基础加强-jdk1.5的一些新特性
JDK 5.0 特性 1.静态导入(import static 语句导入一个类中的某个静态方法或所有静态方法) 如: import static java.lang.Math.*; 2.可变参数 1. ...
- insert buffer/change buffer double write buffer,双写 adaptive hash index(AHI) innodb的crash recovery innodb重要参数 innodb监控
https://yq.aliyun.com/articles/41000 http://blog.itpub.net/22664653/viewspace-1163838/ http://www.cn ...
- 早上好,我是 Istio 1.1
1性能增强 虽然Istio1.0的目标是生产可用,但从去年7月份发布以来,在性能和稳定性上并不能让用户满意.社区的Performance and Scalability工作组在Istio v1.1中做 ...
- Octave基本语法
基本运算 octave:3> 5+6 ans = 11 octave:4> 3-2 ans = 1 octave:5> 8*9 ans = 72 octave:6> 8/4 a ...
- .Nginx安装filebeat收集日志:
1.安装filebeat: [root@nginx ~]# vim /usr/local/filebeat/filebeat.yml [root@nginx ~]# tar xf filebeat-6 ...
- php 的一个异常处理程序
<?php//exceptionHandle.php xiecongwen 20140620 //define('DEBUG',true); /** * Display all errors w ...
- Java8-Lock-No.02
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util ...
- SpringBoot启动过程原理(转)
1.1 Springboot启动: @SpringBootApplication public class ServerApplication { public static void main(St ...
- 更改ejs模板引擎的后缀为html
安装 EJS 在项目目录右键->Open Command Prompt Here 输入 npm install ejs 打开app.js //app.set('view engine', 'ja ...
- python--第五天练习题
# 1.按alist中元素的age由大到小排序 alist = [{'name': 'a', 'age': 20}, {'name': 'b', 'age': 30}, {'name': 'v', ' ...