当前位置:网站首页>合并两个有序列表
合并两个有序列表
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;
}
}
边栏推荐
- 1-hour live broadcast recruitment order: industry big names share dry goods, and enterprise registration opens丨qubit·viewpoint
- ABAP interview questions: how to use the System CALL interface of the ABAP programming language, direct execution ABAP server operating System's shell command?
- GET请求和POST请求区别
- Reading and writing after separation, performance were up 100%
- 信息系统项目管理师必背核心考点(六十三)项目组合管理的主要过程&DIPP分析
- 全面了解什么是TPS、QPS以及两者的区别
- 【Untitled】
- C# 获取系统已安装的.NET版本
- The core key points of microservice architecture
- 虚拟机安装出现的问题汇总
猜你喜欢

曼城推出可检测情绪的智能围巾,把球迷给整迷惑了

LeetCode #101. 对称二叉树

防止数据冒用的方法

#物联网征文#小熊派设备开发实战

Win10 compiles the x264 library (there are also generated lib files)

MongoDB-查询中$all的用法介绍
Byte Qiu Zhao confused me on both sides, and asked me under what circumstances would the SYN message be discarded?

【Untitled】

1小时直播招募令:行业大咖干货分享,企业报名开启丨量子位·视点

Scala 高阶(七):集合内容汇总(上篇)
随机推荐
报告:想学AI的学生数量已涨200%,老师都不够用了
WPF implements a MessageBox message prompt box with a mask
Batch大小不一定是2的n次幂!ML资深学者最新结论
从零开始Blazor Server(9)--修改Layout
软件测试——金融测试类面试题,看完直接去面试了
GPT-3组合DALL·E,60秒内搞定游戏设定和原型动画!网友看后:这游戏想玩
从零开始Blazor Server(9)--修改Layout
proto3-2语法
00后写个暑假作业,被监控成这笔样
Modify the VOT2018.json file and remove the color in the image path
听声辨物,这是AI视觉该干的???|ECCV 2022
ACM longest non-descent subsequence problem
8、IDEA提交代码出现: Fetch failed fatal: Could not read from remote repository
学长告诉我,大厂MySQL都是通过SSH连接的
非科班AI小哥火了:他没有ML学位,却拿到DeepMind的offer
全面了解什么是TPS、QPS以及两者的区别
【无标题】
又有大厂员工连续加班倒下/ 百度搜狗取消快照/ 马斯克生父不为他骄傲...今日更多新鲜事在此...
Manchester city launch emotional intelligence scarf can be detected, give the fans
【面试高频题】可逐步优化的链表高频题