当前位置:网站首页>LeetCode 21. Merge two ordered linked lists
LeetCode 21. Merge two ordered linked lists
2022-08-10 11:58:00 【Mizuna pen】
原题网址:https://leetcode.cn/problems/merge-two-sorted-lists/submissions/
合并两个排好序的链表;遍历节点;Small when the value of the new node;move back after;在比较;
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;
}
边栏推荐
- HDU 4372:Count the Buildings (Stirling数)
- 使用.NET简单实现一个Redis的高性能克隆版(六)
- LeetCode 138. 复制带随机指针的链表
- LeetCode50天刷题计划(Day 16—— 两两交换链表中的节点(9.10-10.30)
- Pulling drills - 56 Finding the right interval
- 微信小程序,全局变量一个地方改变了其他地方的状态也跟着改变。
- 模块九 - 设计电商秒杀系统
- Interviewer: How are Dao, Service, Controller, Util, and Model divided in the project?
- 【机器学习】浅谈正规方程法&梯度下降
- 面试官:你们是如何保证接口的幂等性?
猜你喜欢

常量及数据类型你还记得多少?

快手“弃”有赞与微盟“结亲”,电商SaaS行业竞争格局将变?

StoneDB 文档捉虫活动第一季

负载均衡原理分析与源码解读

ViT结构详解(附pytorch代码)

项目部署、

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

rider内Mono脚本找不到引用资源

Does your child lack self-discipline?Ape Counseling: Pay attention to "blank" in the schedule to give children more control
![[E-commerce operation] Do you really understand social media marketing (SMM)?](/img/5b/6682c613305deb3dc15401077d38a0.png)
[E-commerce operation] Do you really understand social media marketing (SMM)?
随机推荐
【小程序 | 启航篇】一文打通任督二脉
使用哈工大LTP测试分词并且增加自定义字典
三星计划2023年开始在越南生产半导体零部件
零基础想自学软件测试,有没有大佬可以分享下接下来的学习书籍和路线?
传三星3nm斩获第二家客户,目前产能已供不应求
英特尔推送20220809 CPU微码更新 修补Intel-SA-00657安全漏洞
codevs 2370 小机房的树 (LCA)
LeetCode 109. 有序链表转换二叉搜索树
老板加薪!看我做的WPF Loading!!!
jlink 与 swd 接口定义
Analysis of the implementation principle of UUID from the perspective of source code
【LeetCode】640. 求解方程
力扣练习——56 寻找右区间
OSSCore 开源解决方案介绍
老板加薪!看我做的WPF Loading!!!
负载均衡原理分析与源码解读
被面试官问到消息队列的丢失、重复与积压问题该如何回答
Network Fundamentals (Section 1)
Pulling drills - 56 Finding the right interval
十年架构五年生活-09 五年之约如期而至