当前位置:网站首页>合并两个有序列表
合并两个有序列表
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;
}
}
边栏推荐
- 8、IDEA提交代码出现: Fetch failed fatal: Could not read from remote repository
- 【微服务~远程调用】整合RestTemplate、WebClient、Feign
- 虚拟机安装出现的问题汇总
- AQS同步组件-FutureTask解析和用例
- The FFmpeg library is configured and used on win10 (libx264 is not configured)
- Recommend a free 50-hour AI computing platform
- Manchester city launch emotional intelligence scarf can be detected, give the fans
- Too much volume... Tencent was asked on the side that the memory was full, what would happen?
- ACM01 Backpack problem
- MySQL principle and optimization of Group By optimization techniques
猜你喜欢
学长告诉我,大厂MySQL都是通过SSH连接的
非科班AI小哥火了:他没有ML学位,却拿到DeepMind的offer
ABAP 面试题:如何使用 ABAP 编程语言的 System CALL 接口,直接执行 ABAP 服务器所在操作系统的 shell 命令?
你没见过的《老友记》镜头,AI给补出来了|ECCV 2022
The grep command Shell regular expressions, the three musketeers
【小程序】低代码+小游戏=小游戏可视化开发
LeetCode #101. 对称二叉树
阻塞、非阻塞、多路复用、同步、异步、BIO、NIO、AIO 一锅端
Batch大小不一定是2的n次幂!ML资深学者最新结论
Win10 compiles the x264 library (there are also generated lib files)
随机推荐
Scala Advanced (7): Collection Content Summary (Part 1)
基于CAP组件实现补偿事务与幂等性保障
修改VOT2018.json文件,去掉图片路径中的color
Shell之常用小工具(sort、uniq、tr、cut)
曲鸟全栈UI自动化教学(八):框架代码讲解和进一步优化
Summary of learning stages (knapsack problem)
web course design
LeetCode #101. Symmetric Binary Tree
【微服务~远程调用】整合RestTemplate、WebClient、Feign
Blazor Server (9) from scratch -- modify Layout
HAproxy: load balancing
LeetCode热题(11.合并两个有序链表)
The redis library cannot be imported
微信一面:一致性哈希是什么,使用场景,解决了什么问题?
AI篮球裁判火了,走步算得特别准,就问哈登慌不慌
箭头函数和普通函数的常见区别
阿里云新增三大高性能计算解决方案,助力生命科学行业快速发展
We really need DApp?Really can't meet our fantasy App?
告别手摇织布机的AI时代
Two minutes recording can pass by second language!The volcano how to practice and become voice tone reproduction technology?