当前位置:网站首页>21. 合并两个有序链表
21. 合并两个有序链表
2022-07-28 09:53:00 【vv1025】
难度简单1983
将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
示例 1:

输入:l1 = [1,2,4], l2 = [1,3,4] 输出:[1,1,2,3,4,4]
示例 2:
输入:l1 = [], l2 = [] 输出:[]
示例 3:
输入:l1 = [], l2 = [0] 输出:[0]
提示:
- 两个链表的节点数目范围是
[0, 50] -100 <= Node.val <= 100l1和l2均按 非递减顺序 排列
/**
* 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* mergeTwoLists(ListNode* l1, ListNode* l2) {
}
};#include <iostream>
using namespace std;
/// Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
/// Iterative
/// Time Complexity: O(len(l1) + len(l2))
/// Space Complexity: O(1)
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode* dummyHead = new ListNode(-1);
ListNode* p = dummyHead;
ListNode* l1p = l1;
ListNode* l2p = l2;
while(l1p != NULL && l2p != NULL){
if(l1p->val < l2p->val){
p->next = l1p;
l1p = l1p->next;
}
else{
p->next = l2p;
l2p = l2p->next;
}
p = p->next;
}
if(l1p != NULL)
p->next = l1p;
else
p->next = l2p;
ListNode* ret = dummyHead->next;
dummyHead->next = NULL;
delete dummyHead;
return ret;
}
};
int main() {
return 0;
}边栏推荐
- 安装gmp
- Take you to wechat applet development in 3 minutes
- 软件设计师考前20问,注意啦!!
- SizeBasedTriggeringPolicy简介说明
- New features of ES6
- 关于软考高级要不要报班学习
- Fixedwindowrollingpolicy introduction
- MySQL 为什么有时候会选错索引?
- Have you ever seen this kind of dynamic programming -- the stock problem of state machine dynamic programming (Part 2)
- PHP 获取接口的方式
猜你喜欢

Being on duty less than 8 hours a day and being dismissed? Tencent's former employees recovered 13million overtime pay, etc., and the court won a compensation of 90000 in the final judgment

ES (8.1) certification topic
![[esp32][esp idf][lvgl7.9] failed to compile with OLED IIC](/img/16/e5aa43df6ef9bdbc173fa547c85559.png)
[esp32][esp idf][lvgl7.9] failed to compile with OLED IIC

极致通缩和永动机模型,将推动 PlatoFarm 爆发

Flink - checkpoint Failure reason: Not all required tasks are currently running

医药行业数字化建设,箭在弦上

Boss: there are too many systems in the company. Can we realize account interworking?

为报复公司解雇,我更改了项目的所有代码注释!

Installing MySQL for Linux operating system (centos7)

为什么要考一级建造师,一建证书含金量有多高?
随机推荐
LinkedList source massage, ah comfortable
房地产数字化转型方案:全方位数智化系统运营,助力房企管控实效提升
PHP Basics
SeekTiger生态通证STI 新进展,4月14日登录 ZB
DAO社区的胜利,Tiger DAO VC胜在治理与共识
小黑重新站起来看leetcode:653. 两数之和 IV - 输入 BST
PlatoFarm几大创新经济模型,给予当下元宇宙市场的启发
Description of landingsite electronic label quppa firmware entering DFU status
Edge团队详解如何通过磁盘缓存压缩技术提升综合性能体验
关于软考高级要不要报班学习
刚获融资的Espresso Systems,知识产权与团队道德双双陷入困境
arthas使用教程
LandingSite电子标签Quuppa固件进入DFU状态说明
今天和大家聊一聊mysql数据库的数据类型
图解 3 种主流企业架构模式(建议收藏!)
It can traverse all files and subfolders under a folder
matlab特征点提取--记录自用
博弈论 1.Introduction(组合游戏基本概念、对抗搜索、Bash游戏、Nim游戏)
SkiaSharp 之 WPF 自绘 拖曳小球(案例版)
Espresso systems, which has just obtained financing, has both intellectual property rights and team ethics in trouble