当前位置:网站首页>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;
}
};
边栏推荐
- 接口开发规范及测试工具的使用
- Quick sort eight sorts (3) 】 【 (dynamic figure deduction Hoare, digging holes, front and rear pointer method)
- Command line query database
- 5.Set接口与实现类
- Ontology Development Diary 03 - When debugging is in progress
- 5.转换流
- 7.Collections工具类
- class object property method class member
- RPC服务远程漏洞
- Do you know the principles of test cases and how to write defect reports?
猜你喜欢
[Machine Learning] Detailed explanation of web crawler combat
2048小游戏成品源码
WAVE SUMMIT 2022深度学习开发者峰会
通过程序发送 Gmail 邮件
JDBC中的增删改查操作
Ontology Development Diary 03-Understanding Code
电脑硬件基础知识科普
How much do you know about the mobile APP testing process specifications and methods?
实用小技能:一键获取Harbor中镜像信息,快捷查询镜像
seata处理分布式事务
随机推荐
电脑硬件基础知识科普
1. The concept of flow
6. The File types
搭建Tigase进行二次开发
MySQL关于表的知识点,进来学习!
latex中复杂公式换行等号对齐
记录一次被入侵5900端口经历
8.Properties property collection
2.字节流
7.FileFilter接口
真·鸡汤文
3. Coding method
缓存击穿,缓存穿透,缓存雪崩的解释和对应的一些解决方案
MySQL约束关系,你必须要知道的知识点!
6.Map接口与实现类
Ontology Development Diary 03 - When debugging is in progress
pycharm在创建py文件时如何自动注释
条件和递归
免费下载天地图全国基础地理信息矢量数据的一种方法
LeetCode75:颜色分类-C语言一次遍历求解