当前位置:网站首页>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;
}
};边栏推荐
猜你喜欢
随机推荐
2.线程创建
IDEA见过就会爱上的超实用快捷键,一键十行!
1. The concept of flow
3.List接口与实现类
真·鸡汤文
【分布式事务】
【ASM】字节码操作 MethodVisitor 案例实战 生成对象
条件和递归
6. The File types
4. Generics and Utilities
pycharm在创建py文件时如何自动注释
【面试体系知识点总结】---JVM
GBase数据库中,源为 oracle 报出“ORA-01000:超出打开游标最大数”
Super detailed MySQL basic operations
喜迎排名18
[Machine Learning] Basics of Data Science - Basic Practice of Machine Learning (2)
【机器学习】数据科学基础——机器学习基础实践(二)
7.FileFilter interface
Celebrate ranked 18
Secretive present: in modern times

![[Machine Learning] Basics of Data Science - Basic Practice of Machine Learning (2)](/img/9f/5afabec1b9ab1871130c8bf1bae472.png)

![[ASM] Bytecode operation MethodVisitor case combat generation object](/img/a9/df07614f875794d55d530bd04dc476.jpg)





