28-Merge Two Sorted Lists
easy
21. Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
思路:利用map映射,值->指针集合,然后按值从小到大将指针链接起来。时间复杂度O(m+n),空间O(m+n)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
map<int,vector<ListNode*> > maps;
push_into_map(l1,maps);
push_into_map(l2,maps);
ListNode *now=NULL,*pre=NULL,*head;
map<int,vector<ListNode*> >::const_iterator it = maps.begin();
if(it!=maps.end()){
head = (it->second)[0];
}
else head = NULL;
while(it!=maps.end())
{
cout<<(it->second).size()<<endl;
for(int k=0;k<(it->second).size();++k)
{
pre = now;
now = (it->second)[k];
if(pre!=NULL)pre->next = now;
}
it++;
}
return head;
}
void push_into_map(ListNode* l,map<int,vector<ListNode*> > &maps)
{
while(l!=NULL)
{
maps[l->val].push_back(l);
l= l->next;
}
}
};
28-Merge Two Sorted Lists的更多相关文章
- [LeetCode] Merge k Sorted Lists 合并k个有序链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 这 ...
- [LeetCode] Merge Two Sorted Lists 混合插入有序链表
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...
- [LintCode] Merge Two Sorted Lists 混合插入有序链表
Merge two sorted (ascending) linked lists and return it as a new sorted list. The new sorted list sh ...
- No.023:Merge k Sorted Lists
问题: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexit ...
- Merge k Sorted Lists
1. Merge Two Sorted Lists 我们先来看这个 问题: Merge two sorted linked lists and return it as a new list. The ...
- 71. Merge k Sorted Lists
Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
- 【leetcode】Merge k Sorted Lists
Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
- Merge Two Sorted Lists
Merge Two Sorted Lists https://leetcode.com/problems/merge-two-sorted-lists/ Merge two sorted linked ...
- Java for LeetCode 023 Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解 ...
- 【LeetCode练习题】Merge k Sorted Lists
Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
随机推荐
- spring cloud feign的各种配置的使用
在上一节我们完成了feign的基本使用,学会了feign如何去调用其他微服务,这次我们来完成feign的一些自定义配置. 实现功能: 1.全局修改feign的配置和单独修改feign客户端的配 ...
- 单片机stm32串口分析
stm32作为现在嵌入式物联网单片机行业中经常要用多的技术,相信大家都有所接触,今天这篇就给大家详细的分析下有关于stm32的出口,还不是很清楚的朋友要注意看看了哦,在最后还会为大家分享有些关于stm ...
- sort方法和自定义比较器的写法
摘要 在做一些算法题时常常会需要对数组.自定义对象.集合进行排序. 在java中对数组排序提供了Arrays.sort()方法,对集合排序提供Collections.sort()方法.对自定义对象排序 ...
- linux ar
转载:Linux ar命令 | 菜鸟教程 (runoob.com) Linux ar命令用于建立或修改备存文件,或是从备存文件中抽取文件. ar可让您集合许多文件,成为单一的备存文件.在备存文件中,所 ...
- flex步局 11.02
语法 justify-content: flex-start | flex-end | center | space-between | space-around flex-start:弹性盒子元素将 ...
- 使用silky脚手架构建微服务应用
目录 模板简介 构建独立应用的模板Silky.App.Template 构建模块化应用的模板Silky.Module.Template 开源地址 在线文档 模板简介 使用 dotnet new 命令可 ...
- SQL注入之猫舍
第一步:先查看是否存在注入点:构造?id=1 and 1=1 回车后发现页面正常 构造?id=1 and 1=2 发现页面异常,得出结论:存在注入点 第二步:判断字段数 当输入order by 1和o ...
- Centos8上安装Mysql8.X
一.下载Mysql 下载地址:https://dev.mysql.com/downloads/mysql/ 二.将压缩包通过ftp软件服务器的目标位置:并解压 1.我的是放在:/root/softwa ...
- CVE-2020-0796 RCE复现
虽然热度已经过了,之前留的笔记发(水)一篇博客 影响版本 适用于32位系统的Windows 10版本1903 Windows 10 1903版(用于基于x64的系统) Windows 10 1903版 ...
- 图文详解 Java 字节码,让你秒懂全过程
原文地址:https://blog.csdn.net/AliceSmith1/article/details/80051153 即便对那些有经验的Java开发人员来说,阅读已编译的Java字节码也很乏 ...