当前位置:网站首页>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;
}
}
边栏推荐
- P3379 [template] nearest common ancestor (LCA)
- 如何写一个全局的 Notice 组件?
- QT interface nested movement based on qscrollarea
- Flutter Wrap Button bottomNavigationBar学习总结4
- [discrete mathematics review series] III. concept and operation of sets
- 在启牛开户安全么
- Allan方差与随机误差辨识
- Still saying that university rankings are a joke? The latest regulation: Top50 universities in the world can be directly settled in Shanghai!
- CVPR 2022 Oral | SCI:实现快速、灵活与稳健的低光照图像增强
- Operation of simulated examination platform for theoretical question bank of refrigeration and air conditioning equipment operation in 2022
猜你喜欢

Binary tree and figure 1

LeetCode_21(合并两个有序链表)

【离散数学期复习系列】二、一阶逻辑(谓词逻辑)

Flyter page Jump to transfer parameters, tabbar learning summary 5

还在说大学排名是笑话?最新规定:世界top50大学可以直接落户上海!

CVPR 2022 Oral | SCI:实现快速、灵活与稳健的低光照图像增强

2022广东省安全员A证第三批(主要负责人)考试练习题及在线模拟考试

Gin blog summary 1
![[vue/js] realize local caching of variables and objects through localstorage browser (text + full source code)](/img/4d/d6276955277942f96f11df3e4ecd4c.png)
[vue/js] realize local caching of variables and objects through localstorage browser (text + full source code)

数仓的基本概念
随机推荐
Markdown Title centered
[discrete mathematics review series] II. First order logic (predicate logic)
[logodetection dataset processing] (4) extract the logo area of each picture
这个牛逼的低代码生成器,现在开源了!
Allan方差与随机误差辨识
UE5如何将屏幕坐标转为世界坐标和世界方向
Anaconda installs opencv (CV2) and uses it in the jupyter notebook
Do you understand all these difficult memory problems?
Beijing / Shanghai internal promotion | recruitment of full-time interns in the system and network group of Microsoft Research Asia
Simulated 100 questions and online simulated examination for safety production management personnel of hazardous chemical production units in 2022
2022 practice questions and online simulation test for the third batch of Guangdong Provincial Safety Officer a certificate (principal)
【LogoDetection 数据集处理】(4)提取每张图片的logo区域
NC | Wang Jun / song Mozhi combined with third-generation sequencing to analyze the structural variation and function of intestinal flora
Is it safe to open an account in qiniu
几种方式可以实现 JMeter 参数化?
LeetCode_20(括号匹配)
超强实操!手把手教学Kinect深度图与RGB摄像头的标定与配准
Yanrong looks at how to realize the optimal storage solution of data Lake in a hybrid cloud environment
Binary tree and Figure 2
Why should the R & D effectiveness team of Internet companies be independent? When is independence?