当前位置:网站首页>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;
}
};
边栏推荐
猜你喜欢
随机推荐
6.File类
7.FileFilter接口
.equals==
缓存击穿,缓存穿透,缓存雪崩的解释和对应的一些解决方案
米斗APP逆向分析
Go-接口的那些事
安装torch_sparse失败解决方法
Do you know the principles of test cases and how to write defect reports?
通过程序发送 Gmail 邮件
2.Collection interface
oracle查看表空间占用情况并删除多余表所占空间
STM32F103实现IAP在线升级应用程序
Browser error classification
A Practical Guide to Building OWL Ontologies using Protege4 and CO-ODE Tools - Version 1.3 (7.4 Annotation Properties - Annotation Properties)
3. Practice the Thread
日期操作比较全面得代码
LPP code and its comments
Ontology Development Diary 05-Strive to Understand SWRL (Part 2)
Command line query database
可以写进简历的软件测试项目实战经验(包含电商、银行、app等)