当前位置:网站首页>剑指 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;
}
};
边栏推荐
- LeetCode 4. 寻找两个正序数组的中位数(hard)
- Seal Library - installation and introduction
- 只是巧合?苹果iOS16的神秘技术竟然与中国企业5年前产品一致!
- john爆破出現Using default input encoding: UTF-8 Loaded 1 password hash (bcrypt [Blowfish 32/64 X3])
- Leetcode1380: lucky numbers in matrix
- How openharmony starts FA of remote devices
- Just a coincidence? The mysterious technology of apple ios16 is even consistent with the products of Chinese enterprises five years ago!
- 远程办公对我们的各方面影响心得 | 社区征文
- In MySQL and Oracle, the boundary and range of between and precautions when querying the date
- Global and Chinese markets for airport baggage claim conveyors 2022-2028: technology, participants, trends, market size and share Research Report
猜你喜欢
Hard core! One configuration center for 8 classes!
PhD battle-11 preview | review and prospect backdoor attack and defense of neural network
Tech talk activity preview | building intelligent visual products based on Amazon kVs
Serial port controls steering gear rotation
PhD Debate-11 预告 | 回顾与展望神经网络的后门攻击与防御
对接保时捷及3PL EDI案例
Executive engine module of high performance data warehouse practice based on Impala
福元医药上交所上市:市值105亿 胡柏藩身价超40亿
LeetCode 1. 两数之和
pwm呼吸灯
随机推荐
Leetcode1380: lucky numbers in matrix
【征文活动】亲爱的开发者,RT-Thread社区喊你投稿啦
pwm呼吸灯
IP地址转换地址段
对接保时捷及3PL EDI案例
The computer comes with software to make the background color of the picture transparent (matting white background)
社交元宇宙平台Soul冲刺港股:年营收12.8亿 腾讯是股东
Ap和F107数据来源及处理
小鹏P7雨天出事故安全气囊没有弹出 官方回应:撞击力度未达到弹出要求
js删除字符串中的子串
Tech Talk 活动预告 | 基于Amazon KVS打造智能视觉产品
VMware install win10 image
Usage of sprintf() function in C language
什么是泛型?- 泛型入门篇
相信自己,这次一把搞定JVM面试
二、mock平台的扩展
国内比较好的OJ平台[通俗易懂]
Learning Weekly - total issue 60 - 25th week of 2022
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 3. 无重复字符的最长子串