当前位置:网站首页>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;
}
};
边栏推荐
猜你喜欢
福元医药上交所上市:市值105亿 胡柏藩身价超40亿
Cell:清华程功组揭示皮肤菌群的一种气味挥发物促进黄病毒感染宿主吸引蚊虫...
Digital IC hand tearing code -- voting device
OpenHarmony如何启动远程设备的FA
Ap和F107数据来源及处理
Go zero micro service practical series (VIII. How to handle tens of thousands of order requests per second)
PWM breathing lamp
剑指 Offer 21. 调整数组顺序使奇数位于偶数前面
数字IC手撕代码--投票表决器
How to transfer business data with BorgWarner through EDI?
随机推荐
AP and F107 data sources and processing
Exploration of mobile application performance tools
P6774 [NOI2020] 时代的眼泪(分块)
VMware安装win10镜像
Detailed explanation of @accessories annotation of Lombok plug-in
使用知行之桥的API端口,提供资源供合作伙伴访问
Vscode setting delete line shortcut [easy to understand]
OpenPose的使用
【Leetcode】13. 罗马数字转整数
数字IC手撕代码--投票表决器
酒仙网IPO被终止:曾拟募资10亿 红杉与东方富海是股东
VMware install win10 image
13、Darknet YOLO3
綠竹生物沖刺港股:年期內虧損超5億 泰格醫藥與北京亦莊是股東
Youzan won the "top 50 Chinese enterprise cloud technology service providers" together with Tencent cloud and Alibaba cloud [easy to understand]
Executive engine module of high performance data warehouse practice based on Impala
远程办公对我们的各方面影响心得 | 社区征文
Believe in yourself and finish the JVM interview this time
DigiCert SSL证书支持中文域名申请吗?
What will you do after digital IC Verification?