当前位置:网站首页>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;
}
边栏推荐
- 面试官:项目中 Dao、Service、Controller、Util、Model 怎么划分的?
- 老板加薪!看我做的WPF Loading!!!
- [Brave food, not afraid to write the linked list] The problem of the penultimate node of the linked list
- 中小规模网站架构
- 阻塞 非阻塞 poll机制 异步
- 微信小程序提交审核历史版本记录从哪里查看
- 力扣练习——64 最长和谐子序列
- LeetCode_628_三个数的最大乘积
- Chapter 22 Source Code File REST API Reference (4)
- 从产品维度来看 我们为什么不能完全信任Layer2?
猜你喜欢

Get started quickly and conquer three different distributed architecture calling schemes

做自媒体月入几万?博主们都在用的几个自媒体工具

关于“码农”的一点自嘲解构

英特尔推送20220809 CPU微码更新 修补Intel-SA-00657安全漏洞

Intel pushes 20220809 CPU microcode update to patch Intel-SA-00657 security vulnerability

Kyligence 通过 SOC 2 Type II 审计,以可信赖的企业级产品服务全球客户

A case of violent parameter tuning in machine learning

【勇敢饭饭,不怕刷题之链表】链表中有环的问题

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

From the product dimension, why can't we fully trust Layer2?
随机推荐
从源码角度分析UUID的实现原理
gpu-admission 源码分析
力扣练习——62 有效的数独
Licking Exercise - 58 Verifying Binary Search Trees
OPNsense安装配置Zenarmor
VSCode remote connection server error: Could not establish connection to "xxxxxx" possible error reasons and solutions
The brave rice rice, does not fear the brush list of 】 list has a ring
中小规模网站架构
Licking Exercise - 59 From Binary Search Trees to Greater Sum Trees
越折腾越好用的 3 款开源 APP
Will SQL and NoSQL eventually converge?
基于UiAutomator2+PageObject模式开展APP自动化测试实战
LeetCode_152_乘积最大子数组
Intel pushes 20220809 CPU microcode update to patch Intel-SA-00657 security vulnerability
即时零售业态下如何实现自动做账?
态路小课堂丨如何为CXP光模块选择光纤跳线?
HDU 1520 Anniversary party (tree dp)
【勇敢饭饭,不怕刷题之链表】链表中有环的问题
力扣练习——64 最长和谐子序列
ViT结构详解(附pytorch代码)