当前位置:网站首页>每日一题-合并两个有序链表-0720
每日一题-合并两个有序链表-0720
2022-08-05 05:17:00 【菜鸡程序媛】
题目
将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
思路
- 当其中一个链表为空的时候,直接返回另一个链表
- 比较两个链表当前节点的值,小的那个当作链表头节点,next指针指向下一个smaller节点,递归结束后,返回链表头节点
代码
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
if(list1 == null){
return list2;
}
if(list2 == null){
return list1;
}
if(list1.val < list2.val){
list1.next = mergeTwoLists(list1.next, list2);
return list1;
}else{
list2.next = mergeTwoLists(list1, list2.next);
return list2;
}
}
边栏推荐
猜你喜欢
随机推荐
leetCode刷题之第31题
framebuffer应用编程及文字显示(1)
电子产品量产工具(3)- 文字系统实现
PoE视频监控解决方案
面向小白的深度学习代码库,一行代码实现30+中attention机制。
【ts】typescript高阶:typeof使用
电子产品量产工具(2)- 输入系统实现
[Database and SQL study notes] 9. (T-SQL language) Define variables, advanced queries, process control (conditions, loops, etc.)
The University of Göttingen proposed CLIPSeg, a model that can perform three segmentation tasks at the same time
十、视图解析原理与源码分析
Jupyter notebook选择不同的Anaconda环境作为内核运行
【ts】typescript高阶:键值类型及type与interface区别
LeetCode刷题之第74题
[Pytorch study notes] 10. How to quickly create your own Dataset dataset object (inherit the Dataset class and override the corresponding method)
MaskDistill-不需要标注数据的语义分割
You should write like this
2021电赛资源及经验总结
基于STM32F407的WIFI通信(使用的是ESP8266模块)
原型版本管理
OSPF故障排除办法