当前位置:网站首页>LeetCode 21. 合并两个有序链表
LeetCode 21. 合并两个有序链表
2022-08-10 11:09:00 【水菜笔】
原题网址:https://leetcode.cn/problems/merge-two-sorted-lists/submissions/
合并两个排好序的链表;遍历节点;小的当新节点的值;之后后移;在比较;
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
ListNode l1 = list1;
ListNode l2 = list2;
ListNode dummyHead = new ListNode(-1);
ListNode node = dummyHead;
while( l1 != null && l2 != null) {
ListNode newNode = null;
if(l1.val <l2.val) {
newNode = new ListNode(l1.val);
l1 = l1.next;
} else {
newNode = new ListNode(l2.val);
l2 = l2.next;
}
node.next = newNode;
node = newNode;
}
if(l1 != null) {
node.next = l1;
}
if(l2 != null) {
node.next= l2;
}
return dummyHead.next;
}
边栏推荐
- Programmers pursue technology to consolidate basic learning route suggestions
- Hangdian Multi-School-Loop-(uncertainty greedy + line segment tree)
- 面试官:项目中 Dao、Service、Controller、Util、Model 怎么划分的?
- LeetCode50天刷题计划(Day 17—— 下一个序列(14.50-16.30)
- 电脑怎么设置屏幕息屏时间(日常使用分享)
- Licking Exercise - 58 Verifying Binary Search Trees
- Will SQL and NoSQL eventually converge?
- codevs 2370 小机房的树 (LCA)
- gpu-admission 源码分析
- L2 applications from a product perspective: why is it a playground?
猜你喜欢

微信小程序,全局变量一个地方改变了其他地方的状态也跟着改变。

VSCode远程连接服务器报错:Could not establish connection to “xxxxxx”的可能错误原因及解决

mysql出现:ERROR 1524 (HY000): Plugin ‘123‘ is not loaded

【电商运营】你真的了解社交媒体营销(SMM)吗?

Short video software development - how to break the platform homogenization

什么是幂等性?四种接口幂等性方案详解!

中小规模网站架构

Open Office XML 格式里如何描述多段具有不同字体设置的段落

OPNsense安装配置Zenarmor

英特尔推送20220809 CPU微码更新 修补Intel-SA-00657安全漏洞
随机推荐
LeetCode_628_三个数的最大乘积
不止跑路,拯救误操作rm -rf /*的小伙儿
机器学习之暴力调参案例
做自媒体月入几万?博主们都在用的几个自媒体工具
Since the media hot style title how to write?Taught you how to write the title
为什么Redis很快
Kyligence 通过 SOC 2 Type II 审计,以可信赖的企业级产品服务全球客户
推荐6个自媒体领域,轻松易上手
基于UiAutomator2+PageObject模式开展APP自动化测试实战
Licking Exercise - 58 Verifying Binary Search Trees
Stroke Practice - 62 Valid Sudokus
第二十二章 源代码文件 REST API 参考(四)
Licking Exercise - 59 From Binary Search Trees to Greater Sum Trees
The brave rice rice, does not fear the brush list of 】 list has a ring
Where can I view the version record of WeChat applet submission review history?
VSCode远程连接服务器报错:Could not establish connection to “xxxxxx”的可能错误原因及解决
1-IMU参数解析以及选择
不止跑路,拯救误操作rm -rf /*的小伙儿
怎么加入自媒体,了解这5种变现模式,让账号快速变现
HDU 1520 Anniversary party (树型dp)