当前位置:网站首页>剑指 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;
}
};边栏推荐
- Method of C language self defining function
- Exploration and practice of integration of streaming and wholesale in jd.com
- Talk about an experience of job hopping and being rejected
- 深度学习图像数据自动标注[通俗易懂]
- 绿竹生物冲刺港股:年期内亏损超5亿 泰格医药与北京亦庄是股东
- R及RStudio下载安装教程(超详细)
- Résumé de l'entrevue de Dachang Daquan
- 学习周刊-总第60期-2022年第25周
- 隐私计算技术创新及产业实践研讨会:学习
- Yyds dry inventory uses thread safe two-way linked list to realize simple LRU cache simulation
猜你喜欢

The poor family once again gave birth to a noble son: Jiangxi poor county got the provincial number one, what did you do right?

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

Penetration tool - intranet permission maintenance -cobalt strike

Tech talk activity preview | building intelligent visual products based on Amazon kVs

Serial port controls steering gear rotation

In MySQL and Oracle, the boundary and range of between and precautions when querying the date

电脑自带软件使图片底色变为透明(抠图白底)

Linux Installation PostgreSQL + Patroni cluster problem

七张图,学会做有价值的经营分析

Lampe respiratoire PWM
随机推荐
大厂面试总结大全
What will you do after digital IC Verification?
MOSFET器件手册关键参数解读
【Leetcode】14. 最長公共前綴
Global and Chinese market of desktop hot melt equipment 2022-2028: Research Report on technology, participants, trends, market size and share
对接保时捷及3PL EDI案例
TCP congestion control details | 2 background
相信自己,这次一把搞定JVM面试
Exploration and practice of integration of streaming and wholesale in jd.com
LeetCode 3. Longest substring without duplicate characters
Serial port controls steering gear rotation
Global and Chinese markets for airport baggage claim conveyors 2022-2028: technology, participants, trends, market size and share Research Report
Cell:清华程功组揭示皮肤菌群的一种气味挥发物促进黄病毒感染宿主吸引蚊虫...
大廠面試總結大全
John blasting appears using default input encoding: UTF-8 loaded 1 password hash (bcrypt [blowfish 32/64 x3])
What is the difference between JSP and servlet?
几行代码搞定RPC服务注册和发现
机器学习-感知机模型
Penetration tool - intranet permission maintenance -cobalt strike
JS delete substring in string