当前位置:网站首页>LeetCode_ 21 (merge two ordered linked lists)
LeetCode_ 21 (merge two ordered linked lists)
2022-06-10 14:25:00 【***】
Title Description : Merge two ascending linked lists into a new Ascending Link list and return . The new linked list is made up of all the nodes of the given two linked lists .
Example 1:
Input :l1 = [1,2,4], l2 = [1,3,4]
Output :[1,1,2,3,4,4]
Example 2:
Input :l1 = [], l2 = []
Output :[]
Example 3:
Input :l1 = [], l2 = [0]
Output :[0]Tips :
The range of the number of nodes in the two linked lists is [0, 50]
-100 <= Node.val <= 100
l1 and l2 All according to Non decreasing order array
// recursive
/**
* Definition for singly-linked list.
* 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; }
* }
*/
class Solution {
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
if(list1==null)return list2;
if(list2==null)return list1;
// Put the smaller of the two into the new linked list , The other one is called the big one
ListNode list3=list1.val<list2.val?list1:list2;
// Compare the small back one with the just big one
list3.next=mergeTwoLists(list3.next,list1.val>=list2.val?list1:list2);
return list3;
}
}
边栏推荐
- 2022广东省安全员A证第三批(主要负责人)考试练习题及在线模拟考试
- 【LogoDetection 数据集处理】(4)提取每张图片的logo区域
- Shutter wrap button bottomnavigationbar learning summary 4
- 【离散数学期复习系列】五、一些特殊的图
- 如何写一个全局的 Notice 组件?
- C multithreading learning note 4
- 【重庆大学】初试复试资料分享(附考研群)
- Why should the R & D effectiveness team of Internet companies be independent? When is independence?
- Implementation of VGA protocol based on FPGA
- Markdown sets the font to red
猜你喜欢
![[discrete mathematics review series] IV. figure](/img/70/5b2f783265e7e5f6485b49088400da.png)
[discrete mathematics review series] IV. figure
![[logodetection data set processing] (3) divide the training set into multiple folders by category](/img/eb/49c65f9af4c899b8cffaeec630be79.png)
[logodetection data set processing] (3) divide the training set into multiple folders by category

这个牛逼的低代码生成器,现在开源了!

焱融看|混合云环境下,如何实现数据湖最优存储解决方案

anaconda安装opencv(cv2),在jupyter notebook中使用

SIGIR 2022 | 港大、武大提出KGCL:基于知识图谱对比学习的推荐系统

初学者自己搭建博客的设计工具和技巧
![[Chongqing University] information sharing of preliminary and second examinations (with postgraduate entrance examination group)](/img/1d/550a991385b842a21e2b301725407e.png)
[Chongqing University] information sharing of preliminary and second examinations (with postgraduate entrance examination group)

Ue5 how to convert screen coordinates to world coordinates and World Directions
[advanced MySQL] optimize SQL by using the execution plan explain (2)
随机推荐
Allan方差与随机误差辨识
【离散数学期复习系列】四、图
Flutter learning personal summary 1
Do you understand all these difficult memory problems?
2022 Shandong Province safety officer C certificate retraining question bank and online simulation examination
这个牛逼的低代码生成器,现在开源了!
LeetCode_21(合并两个有序链表)
Flutter Icon Stack LIsttitle...学习总结3
Anaconda installs opencv (CV2) and uses it in the jupyter notebook
C multithreading learning note 1
[discrete mathematics review series] II. First order logic (predicate logic)
WordPress的管理员用户名是如何泄露的
.NET C#基础(7):接口 - 人如何和猫互动
QT transfers the received JSON data (including Chinese) Unicode to utf8
[discrete mathematics review series] VI. tree
【原创】POI 5.x XSSF和HSSF使用自定义字体颜色
Celery 异步调用方法改动记录
Redis基本使用1
[logodetection data set processing] (2) draw the label box of the training set picture
Markdown Title centered
