当前位置:网站首页>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)
// }
// }
// }
边栏推荐
猜你喜欢
随机推荐
GBASE 8s 数据库的逻辑日志备份
GBASE 8s 用户标示与鉴别
华东师范大学副校长周傲英:数据赋能,从数据库到数据中台
专利说明书怎么写?
微信小程序如何开通支付功能?
php反序列化结构知识点实例分析
24-hour London gold chart analysis
Small program WeChat positioning is not accurate
网安学习-内网渗透2
数据安全建设
GBASE 8s 如何估算索引使用多少空间
GTK在drawable区域绘图
GBASE 8s自定义存储过程和函数介绍
【LeetCode】36、有效的数独
The implementation of the flood control project and safety construction project in the flood storage and detention areas in Luluze and Ningjinbo was launched
E. XOR Tree(树形dp/异或/最近公共祖先)
Docker 下 Oracle 安装与配置
动态规划专题
Xshell 7 提示 “要继续使用此程序,您必须应用最新的更新或使用新版本”
一文理解分布式开发中的服务治理