当前位置:网站首页>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;
}
边栏推荐
- OSSCore 开源解决方案介绍
- 【Untitled】
- 实现内网穿透的最佳解决方案(无实名认证,完全免费)
- Buckle exercise - rectangular area does not exceed the maximum value of K and (hard)
- 从产品角度看 L2 应用:为什么说这是一个游乐场?
- SQL优化最强总结 (建议收藏~)
- Where can I view the version record of WeChat applet submission review history?
- LeetCode_443_压缩字符串
- 软件架构简介
- Memory problems difficult to locate, it is because you do not use ASAN
猜你喜欢
老板加薪!看我做的WPF Loading!!!
Where can I view the version record of WeChat applet submission review history?
【勇敢饭饭,不怕刷题之链表】链表反转的几种情况
How to join We Media, learn about these 5 monetization modes, and make your account quickly monetize
Get started quickly and conquer three different distributed architecture calling schemes
Nocalhost - Making development more efficient in the cloud-native era
Since the media hot style title how to write?Taught you how to write the title
[Brave food, not afraid of the linked list of brushing questions] Merging of ordered linked lists
即时零售业态下如何实现自动做账?
微信小程序,全局变量一个地方改变了其他地方的状态也跟着改变。
随机推荐
Get started quickly and conquer three different distributed architecture calling schemes
WeChat applet, global variables change in one place and the state in other places also changes.
即时零售业态下如何实现自动做账?
英特尔推送20220809 CPU微码更新 修补Intel-SA-00657安全漏洞
软件架构简介
Some tips for using Unsafe
A little self-deprecating deconstruction about farmers "code"
负载均衡原理分析与源码解读
AUTOCAD——减少样条曲线控制点数、CAD进阶练习(三)
POJ 2891 Strange Way to Express Integers (扩展欧几里得)
CPU多级缓存与缓存一致性
态路小课堂丨如何为CXP光模块选择光纤跳线?
零基础想自学软件测试,有没有大佬可以分享下接下来的学习书籍和路线?
Centos7环境使用Mysql离线安装包安装Mysql5.7
三个绘图工具类详解Paint(画笔)Canvas(画布)Path(路径)
力扣练习——58 验证二叉搜索树
mysql出现:ERROR 1524 (HY000): Plugin ‘123‘ is not loaded
一文带你搞懂中断按键驱动程序之poll机制
从产品维度来看 我们为什么不能完全信任Layer2?
石墨文档打开文档时快速定位到上次写的位置