当前位置:网站首页>Sword finger offer 25 Merge two sorted linked lists
Sword finger offer 25 Merge two sorted linked lists
2022-07-02 17:12:00 【anieoo】
Original link : The finger of the sword Offer 25. Merge two ordered linked lists
solution:
The idea of merging
/**
* 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;
}
};边栏推荐
- 深度之眼(三)——矩阵的行列式
- jsp 和 servlet 有什么区别?
- Exploration of mobile application performance tools
- Learning Weekly - total issue 60 - 25th week of 2022
- 有赞和腾讯云、阿里云一同摘得“中国企业云科技服务商50强”[通俗易懂]
- 深度之眼(二)——矩阵及其基本运算
- PhD Debate-11 预告 | 回顾与展望神经网络的后门攻击与防御
- 学习周刊-总第60期-2022年第25周
- L'explosion de John utilise l'encodage d'entrée par défaut: UTF - 8 Loaded 1 password Hash (bcrypt [blowfish 32 / 64 X3])
- 只是巧合?苹果iOS16的神秘技术竟然与中国企业5年前产品一致!
猜你喜欢

几行代码搞定RPC服务注册和发现

871. 最低加油次数

移动应用性能工具探索之路

Lampe respiratoire PWM

TCP congestion control details | 2 background

数字IC手撕代码--投票表决器

Easy language ABCD sort

Tech Talk 活动预告 | 基于Amazon KVS打造智能视觉产品

Serial port controls steering gear rotation

Configure MySQL under Linux to authorize a user to access remotely, which is not restricted by IP
随机推荐
Connect Porsche and 3PL EDI cases
亚马逊云科技 Community Builder 申请窗口开启
【Leetcode】13. 罗马数字转整数
2020 "Lenovo Cup" National College programming online Invitational Competition and the third Shanghai University of technology programming competition (a sign in, B sign in, C sign in, D thinking +mst
MySQL port
Seal Library - installation and introduction
LeetCode 3. Longest substring without duplicate characters
綠竹生物沖刺港股:年期內虧損超5億 泰格醫藥與北京亦莊是股東
MOSFET器件手册关键参数解读
GeoServer:发布PostGIS数据源
P6774 [NOI2020] 时代的眼泪(分块)
vscode设置删除行快捷键[通俗易懂]
几行代码搞定RPC服务注册和发现
Exploration of mobile application performance tools
深度之眼(三)——矩阵的行列式
jsp 和 servlet 有什么区别?
七张图,学会做有价值的经营分析
Go zero micro service practical series (VIII. How to handle tens of thousands of order requests per second)
Day 18 of leetcode dynamic planning introduction
PhD Debate-11 预告 | 回顾与展望神经网络的后门攻击与防御