当前位置:网站首页>LeetCode(剑指 Offer)- 25. 合并两个排序的链表
LeetCode(剑指 Offer)- 25. 合并两个排序的链表
2022-08-09 09:37:00 【放羊的牧码】
题目链接:点击打开链接
题目大意:略
解题思路:略
相关企业
- 字节跳动
- 微软(Microsoft)
- 谷歌(Google)
- 苹果(Apple)
- 亚马逊(中国)投资有限公司
- 甲骨文(Oracle)
- 彭博(Bloomberg)
- 优步(Uber)
- Indeed
AC 代码
- Java
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode dum = new ListNode(0), cur = dum;
while(l1 != null && l2 != null) {
if(l1.val < l2.val) {
cur.next = l1;
l1 = l1.next;
}
else {
cur.next = l2;
l2 = l2.next;
}
cur = cur.next;
}
cur.next = l1 != null ? l1 : l2;
return dum.next;
}
}- C++
/**
* 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* dum = new ListNode(0);
ListNode* cur = dum;
while(l1 != nullptr && l2 != nullptr) {
if(l1->val < l2->val) {
cur->next = l1;
l1 = l1->next;
}
else {
cur->next = l2;
l2 = l2->next;
}
cur = cur->next;
}
cur->next = l1 != nullptr ? l1 : l2;
return dum->next;
}
};边栏推荐
猜你喜欢
随机推荐
makefile学习-解决目标文件输出路径问题
LPP code and its comments
字典
【ASM】字节码操作 MethodVisitor 案例实战 生成对象
4.泛型和工具类
【八大排序①】插入排序(直接插入排序、希尔排序)
nacos从下载到安装集群的
【个人学习总结】CRC校验原理及实现
LeetCode75:颜色分类-C语言一次遍历求解
Ontology development diary 02 - simple sparql query
1. Introduction to threads
ORA-00600 [16703], [1403], [20]问题分析及恢复
多线程案例——定时器
迭代
3.List interface and implementation class
可以写进简历的软件测试项目实战经验(包含电商、银行、app等)
3.练习Thread
3.List接口与实现类
关于一次性通过CISSP考试的一点经验分享
Ontology development diary 04 - to try to understand some aspects of protege









![[Machine Learning] Detailed explanation of web crawler combat](/img/ac/f00f0c81e66ba526ac39ee60fad72b.png)