当前位置:网站首页>JZ25 合并两个排序的链表
JZ25 合并两个排序的链表
2022-07-20 21:38:00 【syc596】
JZ25 合并两个排序的链表
合并两个排序的链表_牛客题霸_牛客网 (nowcoder.com)
// //递归
// public class Solution {
// public ListNode Merge(ListNode head1,ListNode head2) {
// if(head1==null){
// return head2;
// }
// if(head2==null){
// return head1;
// }
// ListNode newHead=null;
// if(head1.val<head2.val){
// newHead=head1;
// head1=head1.next;
// }else{
// newHead=head2;
// head2=head2.next;
// }
// newHead.next=Merge(head1,head2);
// return newHead;
// }
// }
//二路归并
public class Solution {
public ListNode Merge(ListNode head1,ListNode head2) {
if(head1==null){
return head2;
}
if(head2==null){
return head1;
}
ListNode newHead=new ListNode(-1);
ListNode cur=newHead;
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;
}
if(head2!=null){
cur.next=head2;
}
return newHead.next;
}
}
边栏推荐
- 【微信小程序】switch开关选择器(81/100)
- Multi thread anti conflict
- Dam Chapter 13 (data quality management)
- Why is sha256 irreversible and how secure is sha256
- Project summary: document management system
- In the e-commerce industry with fierce competition, why can the chain 2+1 mode survive for so long?
- [JS] tab switching
- Wechat vaccine appointment applet graduation design of applet completion work (8) graduation design thesis template
- How can traditional retail enterprises promote consumption cycle and share purchase mode to enable the development of physical enterprises?
- i2c client驱动
猜你喜欢
In the e-commerce industry with fierce competition, why can the chain 2+1 mode survive for so long?
这款国产API神器工具也太强了吧...让我放弃了postman
Wechat applet - "data binding and event binding"
[JS] dynamic rendering page
Dama Chapter 11 (data warehouse and business intelligence)
conda虚拟环境默认路径如何修改
c || 文件操作
目前最火的测试框架,pytest封神级讲解
C | array and pointer
传统零售企业如何促进消费循环、分享购模式赋能实体企业发展?
随机推荐
L'ipaylinks, un service d'intégration des paiements transfrontaliers, a remporté le prix 3A Asia de l'actif!
i2c client驅動之IIO設備驅動
Wechat applet - "data binding and event binding"
Bank data management measures
i2c client驱动
Scanning code for vulnerabilities -- those that need to be rectified
I2C client Drive IIO Device Drive
One Oracle statement to update or add
ROS from entry to mastery (x) TF coordinate transformation principle, why do you need TF transformation?
2.3 branch statements
Question quotidienne - leetcode1260 - migration de grille 2D - Tableau - Cartographie
Use priority queue_ Queue implements unordered to base_ Value filtering of map on value value value
Wechat vaccine appointment applet graduation design (5) assignment book of applet completion work
Origin 2022 download nanny level installation tutorial
[JS] tab switching
TransUNet: Transformers Make Strong Encoders for Medical Image Segmentation
Shape and boundary-aware multi-branch model for semi-supervised medical image segmentation
Wechat vaccine appointment applet graduation project of applet completion work (7) mid term inspection report
Upload a folder (including subfolders and files) in VSS
BluePrism工具菜单栏用户权限功能描述-RPA第二章