当前位置:网站首页>合并两个有序列表
合并两个有序列表
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;
}
}
边栏推荐
- Win10 compiles the x264 library (there are also generated lib files)
- 中科院打脸谷歌:普通电脑追上量子优越性,几小时搞定原本要一万年的计算...
- 问题来了:4GB物理内存的机器上申请8G内存能成功吗?
- Double pointer - the role of char **, int **
- WeChat payment development process
- electron 应用开发优秀实践
- Ways to prevent data fraud
- shell脚本------函数的格式,传参,变量,递归,数组
- The batch size does not have to be a power of 2!The latest conclusions of senior ML scholars
- API调用,API传参,面向对接开发,你真的会写接口文档吗?
猜你喜欢
随机推荐
读写分离后,性能居然提升100%了呀
Recommend a free 50-hour AI computing platform
ABP 6.0.0-rc.1的新特性
微信小程序支付及退款整体流程
放下手机吧:实验表明花20分钟思考和上网冲浪同样快乐
你没见过的《老友记》镜头,AI给补出来了|ECCV 2022
太卷了... 腾讯一面被问到内存满了,会发生什么?
Modify the VOT2018.json file and remove the color in the image path
Two ways to enter the Oracle database
web course design
荣耀携手Blue Yonder,加快企业战略增长
西湖大学教授怎么看AI制药革命?|量子位智库圆桌实录
goalng-sync/atomic原子操作
在北极都可以穿短袖了,温度飙升至32.5℃
国产抗新冠口服药每瓶不超300元/ 我国IPv6网络全面建成/ 谷歌入局折叠屏手机...今日更多新鲜事在此...
The batch size does not have to be a power of 2!The latest conclusions of senior ML scholars
PM2之配置文件
win10编译x264库(也有生成好的lib文件)
Blazor Server (9) from scratch -- modify Layout
The FFmpeg library is configured and used on win10 (libx264 is not configured)