当前位置:网站首页>剑指 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;
}
};边栏推荐
- The impact of telecommuting on all aspects of our experience | community essay solicitation
- js删除字符串中的子串
- AP and F107 data sources and processing
- 配置基于接口的ARP表项限制和端口安全(限制用户私自接入傻瓜交换机或非法主机接入)
- 你想要的宏基因组-微生物组知识全在这(2022.7)
- Exploration of mobile application performance tools
- 2322. 从树中删除边的最小分数(异或和&模拟)
- Seven charts, learn to do valuable business analysis
- How openharmony starts FA of remote devices
- 移动应用性能工具探索之路
猜你喜欢

【Leetcode】13. 罗马数字转整数

机器学习-感知机模型

Dgraph: large scale dynamic graph dataset

Interview summary of large factories

Interpretation of key parameters in MOSFET device manual

DGraph: 大规模动态图数据集

Masa framework - DDD design (1)

关于举办科技期刊青年编辑沙龙——新时代青年编辑应具备的能力及提升策略的通知...

宝宝巴士创业板IPO被终止:曾拟募资18亿 唐光宇控制47%股权

隐私计算技术创新及产业实践研讨会:学习
随机推荐
只是巧合?苹果iOS16的神秘技术竟然与中国企业5年前产品一致!
System Verilog implements priority arbiter
Error when uploading code to remote warehouse: remote origin already exists
Yolov5 practice: teach object detection by hand
Seal Library - installation and introduction
Ap和F107数据来源及处理
C语言自定义函数的方法
超卓航科上市:募资9亿市值超60亿 成襄阳首家科创板企业
R and rstudio download and installation tutorial (super detailed)
Global and Chinese market of discharge machines 2022-2028: Research Report on technology, participants, trends, market size and share
The computer comes with software to make the background color of the picture transparent (matting white background)
jsp 和 servlet 有什么区别?
Digital IC hand tearing code -- voting device
上传代码到远程仓库报错error: remote origin already exists.
[error record] error -32000 received from application: there are no running service protocol
LeetCode 6. Zigzag transformation (n-shaped transformation)
JS delete substring in string
Dgraph: large scale dynamic graph dataset
Global and Chinese market of oil analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
Executive engine module of high performance data warehouse practice based on Impala