当前位置:网站首页>JZ25 合并两个排序的链表
JZ25 合并两个排序的链表
2022-07-29 21:49:00 【syc596】
JZ25 合并两个排序的链表
合并两个排序的链表_牛客题霸_牛客网 (nowcoder.com)
NC33 合并两个排序的链表
合并两个排序的链表_牛客题霸_牛客网 (nowcoder.com)
//11
//迭代
//二路归并
import java.util.*;
public class Solution {
public ListNode Merge(ListNode head1,ListNode head2) {
if(head1==null){
return head2;
}
if(head2==null){
return head1;
}
ListNode vhead=new ListNode(-1);
ListNode cur=vhead;
while(head1!=null&&head2!=null){
if(head1.val<head2.val){
cur.next=head1;
head1=head1.next;
cur=cur.next;
}else{
cur.next=head2;
head2=head2.next;
cur=cur.next;
}
}
if(head1!=null){
cur.next=head1;
}else{
cur.next=head2;
}
return vhead.next;
}
}
// //递归?
// import java.util.*;
// public class Solution {
// public ListNode Merge(ListNode head1,ListNode head2) {
// if(head1==null){
// return head2;
// }
// if(head2==null){
// return head1;
// }
// if(head1.val<head2.val){
// head1.next=Merge(head1.next,head2);
// return head1;
// }else{
// head2.next=Merge(head1,head2.next)
// }
// }
// }边栏推荐
- 一篇关于Web3.0如何走向安全时代的说明
- VSCode 插件大全
- Huawei Enjoy 50 Pro evaluation: HarmonyOS blessing is smoother and safer
- Small program WeChat positioning is not accurate
- 【板栗糖GIS】wps—如何查看表格中的超链接
- ict的终极模式 是软件研发
- The implementation of the flood control project and safety construction project in the flood storage and detention areas in Luluze and Ningjinbo was launched
- 【板栗糖GIS】arcmap—如何快捷替换属性表中的部分内容
- 官宣!苏州吴江开发区上线电子劳动合同平台
- 网安学习-内网渗透2
猜你喜欢
随机推荐
Advanced Mathematics (Seventh Edition) Tongji University Exercises 3-8 Individual Answers
网络通信编程基础,BIO,NIO
03-树3 Tree Traversals Again(树的遍历)
研究生怎么申请专利,流程是什么?
5V升压充电8.4V芯片
七、HikariConfig初始化分析
[Point Cloud] M3DeTR: Multi-representation, Multi-scale, Mutual-relation 3D Object Detection with Transformers
Xshell 7 提示 “要继续使用此程序,您必须应用最新的更新或使用新版本”
模型评价指标汇总(持续更新)
网安学习-内网渗透2
新库上线 | CnOpenData租赁和商务服务业工商注册企业基本信息数据
GBASE 8s 通过临时表提升排序性能
GBASE 8s 数据库复合索引
转:idea中language level设置
SQL 改写系列七:谓词移动
leetcode122. Best Time to Buy and Sell Stock II 买卖股票的最佳时机 II(简单)
小程序微信定位不准
【点云】M3DeTR: Multi-representation, Multi-scale, Mutual-relation 3D Object Detection with Transformers
Docker 下 Oracle 安装与配置
GBASE 8s 数据库的大对象和日志备份









