当前位置:网站首页>合并两个有序列表
合并两个有序列表
2022-08-09 12:01:00 【爱敲代码的Harrison】
题目
力扣链接:合并两个有序链表

代码
package com.harrison.class06;
/** * @author Harrison * @create 2022-06-19-17:09 * @motto 众里寻他千百度,蓦然回首,那人却在灯火阑珊处。 */
public class Code09_MergeTwoSortedLists {
public class ListNode {
int val;
ListNode next;
ListNode() {
}
ListNode(int val) {
this.val = val;
}
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}
public ListNode mergeTwoLists(ListNode head1, ListNode head2) {
if(head1==null || head2==null){
return head1==null?head2:head1;
}
ListNode head=head1.val<=head2.val?head1:head2;
ListNode cur1=head.next;
ListNode cur2=head==head1?head2:head1;
ListNode pre=head;
while(cur1!=null && cur2!=null){
if(cur1.val<=cur2.val){
pre.next=cur1;
cur1=cur1.next;
}else{
pre.next=cur2;
cur2=cur2.next;
}
pre=pre.next;
}
pre.next=cur1!=null?cur1:cur2;
return head;
}
}
边栏推荐
- ARP协议原理
- 张朝阳对话俞敏洪:一边是手推物理公式,一边是古诗信手拈来
- GRPC整体学习
- 用皮肤“听”音乐,网友戴上这款装备听音乐会:仿佛住在钢琴里
- Batch大小不一定是2的n次幂!ML资深学者最新结论
- ABAP interview questions: how to use the System CALL interface of the ABAP programming language, direct execution ABAP server operating System's shell command?
- 我们真的需要DApp吗?App真的不能满足我们的幻想吗?
- WPF 实现带蒙版的 MessageBox 消息提示框
- 数字化转型之支撑保障单元
- "Digital Economy Panorama White Paper" Special Analysis of Banking Industry Intelligent Marketing Application Released
猜你喜欢

超越CLIP的多模态模型,只需不到1%的训练数据!南加大最新研究来了

ABP 6.0.0-rc.1的新特性

GRPC整体学习

【小程序】低代码+小游戏=小游戏可视化开发

专业人士使用的 11 种渗透测试工具

Reading and writing after separation, performance were up 100%

AQS同步组件-FutureTask解析和用例

腾讯欲成育碧最大股东/ 米哈游招NLP内容生成研究员/ AI发现四千余物种濒临灭绝...今日更多新鲜事在此...

Too much volume... Tencent was asked on the side that the memory was full, what would happen?

听声辨物,这是AI视觉该干的???|ECCV 2022
随机推荐
FFmpeg在win10上编译安装(配置libx264)
智驾科技完成C1轮融资,此前2轮已融4.5亿元
网页控制台控制编辑框
FFmpeg compiles and installs on win10 (configure libx264)
Win10 compiles the x264 library (there are also generated lib files)
Experiment record: the process of building a network
基于CAP组件实现补偿事务与幂等性保障
OpenSSF的开源软件风险评估工具:Scorecards
Adalvo acquires its first branded product, Onsolis
Batch大小不一定是2的n次幂!ML资深学者最新结论
900页数学论文证明旋转的黑洞不会爆炸,丘成桐:30多年来广义相对论首次重大突破...
Here comes the question: Can I successfully apply for 8G memory on a machine with 4GB physical memory?
你没见过的《老友记》镜头,AI给补出来了|ECCV 2022
LeetCode热题(11.合并两个有序链表)
【微服务~远程调用】整合RestTemplate、WebClient、Feign
实验记录:搭建网络过程
微信支付开发流程
基于STM32+铂电阻设计的测温仪
Ways to prevent data fraud
字节秋招二面把我干懵了,问我SYN报文什么情况下会被丢弃?