当前位置:网站首页>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;
}
边栏推荐
- LeetCode 86. Delimited Linked List
- Network sockets (UDP and TCP programming)
- Buckle exercise - rectangular area does not exceed the maximum value of K and (hard)
- 3款不同类型的自媒体免费工具,有效提高创作、运营效率
- HDU 6040 Hints of sd0061 (技巧)
- Licking Exercise - 60 Maximum key-value sum of binary search subtrees
- LeetCode 109. 有序链表转换二叉搜索树
- Does your child lack self-discipline?Ape Counseling: Pay attention to "blank" in the schedule to give children more control
- LeetCode 86. 分隔链表
- Analysis of the implementation principle of UUID from the perspective of source code
猜你喜欢

零基础想自学软件测试,有没有大佬可以分享下接下来的学习书籍和路线?

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

Network sockets (UDP and TCP programming)

负载均衡原理分析与源码解读
![[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)?

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

推荐6个自媒体领域,轻松易上手

LeetCode50天刷题计划(Day 18—— 搜索旋转排序数组(8.50-12.00)

从源码角度分析UUID的实现原理

态路小课堂丨如何为CXP光模块选择光纤跳线?
随机推荐
Flutter气泡框实现
微信小程序,全局变量一个地方改变了其他地方的状态也跟着改变。
机器学习之暴力调参案例
Analysis of the name matching process between the LCD driver and the device (Tiny4412)
使用.NET简单实现一个Redis的高性能克隆版(六)
传三星3nm斩获第二家客户,目前产能已供不应求
即时零售业态下如何实现自动做账?
负载均衡原理分析与源码解读
Clicking Exercise - 64 Longest Harmonic Subsequences
【mysql】explain介绍[通俗易懂]
CPU多级缓存与缓存一致性
codevs 2370 小机房的树 (LCA)
微信小程序提交审核历史版本记录从哪里查看
力扣练习——63 找到字符串中所有字母异位词
配置swagger
LeetCode 369. Plus One Linked List
【小程序 | 启航篇】一文打通任督二脉
LeetCode 83. 删除排序链表中的重复元素
推荐6个自媒体领域,轻松易上手
LeetCode50天刷题计划(Day 16—— 两两交换链表中的节点(9.10-10.30)