当前位置:网站首页>剑指 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;
}
};边栏推荐
- 大廠面試總結大全
- Learning Weekly - total issue 60 - 25th week of 2022
- LeetCode 6. Zigzag transformation (n-shaped transformation)
- Global and Chinese markets for airport baggage claim conveyors 2022-2028: technology, participants, trends, market size and share Research Report
- Linux Installation PostgreSQL + Patroni cluster problem
- PWM控制舵机
- Method of C language self defining function
- Deep learning image data automatic annotation [easy to understand]
- 深度学习图像数据自动标注[通俗易懂]
- LeetCode 4. Find the median (hard) of two positive arrays
猜你喜欢

Yolov5 practice: teach object detection by hand

超卓航科上市:募资9亿市值超60亿 成襄阳首家科创板企业

linux安装postgresql + patroni 集群问题

基于多元时间序列对高考预测分析案例

Machine learning perceptron model

一文看懂:数据指标体系的4大类型

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

⌈ 2022 ⌋ how to use webp gracefully in projects

易语言abcd排序

Cell: Tsinghua Chenggong group revealed an odor of skin flora. Volatiles promote flavivirus to infect the host and attract mosquitoes
随机推荐
学习周刊-总第60期-2022年第25周
What is normal distribution? What is the 28 law?
LeetCode 3. Longest substring without duplicate characters
How openharmony starts FA of remote devices
Résumé de l'entrevue de Dachang Daquan
畅玩集团冲刺港股:年营收2.89亿 刘辉有53.46%投票权
LeetCode 4. Find the median (hard) of two positive arrays
Just a coincidence? The mysterious technology of apple ios16 is even consistent with the products of Chinese enterprises five years ago!
绿竹生物冲刺港股:年期内亏损超5亿 泰格医药与北京亦庄是股东
linux安装postgresql + patroni 集群问题
TCP server communication process (important)
GeoServer:发布PostGIS数据源
如何与博格华纳BorgWarner通过EDI传输业务数据?
对接保时捷及3PL EDI案例
Download blender on Alibaba cloud image station
A week of short video platform 30W exposure, small magic push helps physical businesses turn losses into profits
Interpretation of key parameters in MOSFET device manual
unity Hub 登錄框變得很窄 無法登錄
[error record] error -32000 received from application: there are no running service protocol
入行数字IC验证后会做些什么?