当前位置:网站首页>剑指 Offer 25. 合并两个排序的链表
剑指 Offer 25. 合并两个排序的链表
2022-07-02 14:21:00 【anieoo】
solution:
归并的思想
/**
* 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) {
ListNode *dummy = new ListNode(-1);
ListNode *cur = dummy;
while(l1 != NULL && l2 != NULL) {
if(l1->val <= l2->val) {
cur->next = new ListNode(l1->val);
cur = cur->next;
l1 = l1->next;
} else {
cur->next = new ListNode(l2->val);
cur = cur->next;
l2 = l2->next;
}
}
if(l1 != NULL) cur->next = l1;
if(l2 != NULL) cur->next = l2;
return dummy->next;
}
};边栏推荐
- [error record] the connection of the flutter device shows loading (disconnect | delete the shuttle/bin/cache/lockfile file)
- 隐私计算技术创新及产业实践研讨会:学习
- 社交元宇宙平台Soul冲刺港股:年营收12.8亿 腾讯是股东
- JS delete substring in string
- 【征文活动】亲爱的开发者,RT-Thread社区喊你投稿啦
- [North Asia data recovery] data recovery case of raid crash caused by hard disk disconnection during data synchronization of hot spare disk of RAID5 disk array
- john爆破出現Using default input encoding: UTF-8 Loaded 1 password hash (bcrypt [Blowfish 32/64 X3])
- Day 18 of leetcode dynamic planning introduction
- &lt;四&gt; H264解码输出yuv文件
- LeetCode 2. Add two numbers
猜你喜欢

Notice on holding a salon for young editors of scientific and Technological Journals -- the abilities and promotion strategies that young editors should have in the new era

【Leetcode】14. 最长公共前缀

Linux Installation PostgreSQL + Patroni cluster problem

社交元宇宙平台Soul冲刺港股:年营收12.8亿 腾讯是股东

DigiCert SSL证书支持中文域名申请吗?

Interview summary of large factories

LeetCode 1. 两数之和

如何与博格华纳BorgWarner通过EDI传输业务数据?

Day 18 of leetcode dynamic planning introduction

Seven charts, learn to do valuable business analysis
随机推荐
P6774 [noi2020] tears in the era (block)
Executive engine module of high performance data warehouse practice based on Impala
Tech talk activity preview | building intelligent visual products based on Amazon kVs
有赞和腾讯云、阿里云一同摘得“中国企业云科技服务商50强”[通俗易懂]
System Verilog implements priority arbiter
vscode设置删除行快捷键[通俗易懂]
Interview summary of large factories
Role and function of uboot
What will you do after digital IC Verification?
你想要的宏基因组-微生物组知识全在这(2022.7)
P6774 [NOI2020] 时代的眼泪(分块)
&lt;四&gt; H264解码输出yuv文件
LeetCode 4. 寻找两个正序数组的中位数(hard)
LeetCode 4. Find the median (hard) of two positive arrays
Global and Chinese market of oil analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
John blasting appears using default input encoding: UTF-8 loaded 1 password hash (bcrypt [blowfish 32/64 x3])
易语言abcd排序
Global and Chinese markets for carbon dioxide laser cutting heads 2022-2028: Research Report on technology, participants, trends, market size and share
亚马逊云科技 Community Builder 申请窗口开启
Yyds dry inventory uses thread safe two-way linked list to realize simple LRU cache simulation