当前位置:网站首页>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;
}
边栏推荐
- Centos7环境使用Mysql离线安装包安装Mysql5.7
- Not just running away, but saving the guy who mishandled rm -rf /*
- How many constants and data types do you remember?
- 使用.NET简单实现一个Redis的高性能克隆版(六)
- 什么是幂等性?四种接口幂等性方案详解!
- 学长告诉我,大厂MySQL都是通过SSH连接的
- 迈矽科推出高性能77GHz毫米波雷达芯片,尚未量产就已获数万颗订单
- A case of violent parameter tuning in machine learning
- 皕杰报表在传参乱码
- 微信小程序提交审核历史版本记录从哪里查看
猜你喜欢
随机推荐
网络基础(第一节)
LeetCode 146. LRU 缓存
Licking Exercise - 63 Find all anagrams in a string
Do self-media monthly income tens of thousands?Several self-media tools that bloggers are using
Analysis of the implementation principle of UUID from the perspective of source code
[Go WebSocket] 多房间的聊天室(一)思考篇
Ssm framework construction process [easy to understand]
可视化服务编排在金融APP中的实践
10 个 Reduce 常用“奇技淫巧”
ViT结构详解(附pytorch代码)
力扣练习——62 有效的数独
It is rumored that Samsung 3nm has won the second customer, and the current production capacity is in short supply
LeetCode 369. Plus One Linked List(链表加1)
ssm框架搭建过程[通俗易懂]
Buckle exercise - rectangular area does not exceed the maximum value of K and (hard)
Centos7环境使用Mysql离线安装包安装Mysql5.7
LeetCode 83. 删除排序链表中的重复元素
gpu-admission 源码分析
HDU 4372:Count the Buildings (Stirling数)
OPNsense安装配置Zenarmor