当前位置:网站首页>【Hot100】21. 合并两个有序链表
【Hot100】21. 合并两个有序链表
2022-07-02 18:54:00 【王六六的IT日常】
21. 合并两个有序链表
简单题
将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
递归
发现有意思的一个图哈哈哈哈哈
- 终止条件:当两个链表都为空时
- 如何递归:我们判断list1 和 list2 的头结点哪个更小,然后较小结点的 next 指针指向其余结点的合并结果。(调用递归)
- 如果 list1 的 val 值更小,则将 list1 .next 与排序好的链表头相接,list2同理
- 返回值:每一层调用都返回排序好的链表头
/** * 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;
}
else if (list2 == null) {
return list1;
}
else if (list1.val < list2.val) {
list1.next = mergeTwoLists(list1.next, list2);
return list1;
}
else {
list2.next = mergeTwoLists(list1, list2.next);
return list2;
}
}
}
- 迭代
private ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode dummyHead = new ListNode(0);
ListNode tail = dummyHead;
while (l1 != null && l2 != null) {
if (l1.val < l2.val) {
tail.next = l1;
l1 = l1.next;
} else {
tail.next = l2;
l2 = l2.next;
}
tail = tail.next;
}
tail.next = l1 == null? l2: l1;
return dummyHead.next;
}
边栏推荐
- 有时候只查询一行语句,执行也慢
- AcWing 383. Sightseeing problem solution (shortest circuit)
- AcWing 1131. 拯救大兵瑞恩 题解(最短路)
- After writing 100000 lines of code, I sent a long article roast rust
- Start practicing calligraphy
- NMF-matlab
- Registration opportunity of autowiredannotationbeanpostprocessor in XML development mode
- 浏览器缓存机制概述
- PXE installation "recommended collection"
- Think about the huge changes caused by variables
猜你喜欢
[NLP] a detailed generative text Abstract classic paper pointer generator
勵志!大凉山小夥全獎直博!論文致謝看哭網友
字典
嵌入式(PLD) 系列,EPF10K50RC240-3N 可编程逻辑器件
sql-labs
KT148A语音芯片ic的用户端自己更换语音的方法,上位机
搭建主从模式集群redis
Idea editor removes SQL statement background color SQL statement warning no data sources are configured to run this SQL And SQL dialect is not config
450 Shenxin Mianjing 1
SQLite 3.39.0 release supports right external connection and all external connection
随机推荐
Registration opportunity of autowiredannotationbeanpostprocessor under annotation development mode
在消费互联网时代,诞生了为数不多的头部平台的话
Detailed tutorial on installing stand-alone redis
Génération automatique de fichiers d'annotation d'images vgg
Detailed explanation of VBScript (I)
R语言使用econocharts包创建微观经济或宏观经济图、indifference函数可视化无差异曲线(indifference curve)
编写完10万行代码,我发了篇长文吐槽Rust
Development skills of rxjs observable custom operator
c语言里怎么设立优先级,细说C语言优先级
Codeforces Round #802 (Div. 2) 纯补题
Postman download and installation
【NLP】一文详解生成式文本摘要经典论文Pointer-Generator
自動生成VGG圖像注釋文件
搭建哨兵模式reids、redis从节点脱离哨兵集群
Windows2008r2 installing php7.4.30 requires localsystem to start the application pool, otherwise 500 error fastcgi process exits unexpectedly
AcWing 181. Turnaround game solution (search ida* search)
AcWing 903. Expensive bride price solution (the shortest path - building map, Dijkstra)
Kt148a voice chip instructions, hardware, protocols, common problems, and reference codes
Think about the huge changes caused by variables
AcWing 1129. Heat wave solution (shortest path SPFA)