当前位置:网站首页>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;
}
边栏推荐
- Alibaba最新神作!耗时182天肝出来1015页分布式全栈手册太香了
- 从脚本到剪辑,影像大师亲授的后期制作秘籍
- 基于UiAutomator2+PageObject模式开展APP自动化测试实战
- HDU 4372:Count the Buildings (Stirling数)
- mysql出现:ERROR 1524 (HY000): Plugin ‘123‘ is not loaded
- POJ 1026 Cipher (置换群)
- 暑期总结4
- gpu-admission 源码分析
- Double.doubleToLongBits() method uses
- The brave rice rice, does not fear the brush list of 】 list has a ring
猜你喜欢
LeetCode50天刷题计划(Day 19—— 在排序数组中查找元素的第一个和最后一个位置(9.10-10.40)
Nocalhost - 让云原生时代的开发更高效
[Brave food, not afraid to write the linked list] The problem of the penultimate node of the linked list
为什么Redis很快
建校仅11年就入选“双一流” ,这所高校是凭什么做到的?
[Brave food, not afraid of the linked list of brushing questions] Merging of ordered linked lists
A case of violent parameter tuning in machine learning
从源码角度分析UUID的实现原理
使用哈工大LTP测试分词并且增加自定义字典
[E-commerce operation] Do you really understand social media marketing (SMM)?
随机推荐
StoneDB Document Bug Hunting Season 1
接口定义与实现
Programmers pursue technology to consolidate basic learning route suggestions
力扣练习——60 二叉搜索子树的最大键值和
OPNsense安装配置Zenarmor
HDU 1520 Anniversary party (tree dp)
Kyligence 通过 SOC 2 Type II 审计,以可信赖的企业级产品服务全球客户
态路小课堂丨如何为CXP光模块选择光纤跳线?
第2章-矩阵及其运算-矩阵创建(1)
关于“码农”的一点自嘲解构
力扣练习——59 从二叉搜索树到更大和树
MLX90640 红外热成像仪测温传感器 手机 APP 软件 RedEye 连接详细
力扣练习——64 最长和谐子序列
Clicking Exercise - 64 Longest Harmonic Subsequences
POJ 1026 Cipher (Permutation Groups)
微信小程序,全局变量一个地方改变了其他地方的状态也跟着改变。
Codeforces 814 C. An impassioned circulation of affection (dp)
Nocalhost - 让云原生时代的开发更高效
Spss-多元回归案例实操
负载均衡原理分析与源码解读