当前位置:网站首页>【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;
}
边栏推荐
- 自動生成VGG圖像注釋文件
- One side is volume, the other side is layoff. There are a lot of layoffs in byte commercialization department. What do you think of this wave?
- Design and implementation of ks004 based on SSH address book system
- 定了,就是它!
- 自动生成VGG图像注释文件
- Automatic reading of simple books
- JS如何取整数
- After writing 100000 lines of code, I sent a long article roast rust
- Postman download and installation
- AcWing 1126. 最小花费 题解(最短路—dijkstra)
猜你喜欢

KS004 基于SSH通讯录系统设计与实现

蓝牙芯片ble是什么,以及该如何选型,后续技术发展的路径是什么

搭建主从模式集群redis

450-深信服面经1

勵志!大凉山小夥全獎直博!論文致謝看哭網友

KT148A语音芯片ic的硬件设计注意事项

SQLite 3.39.0 发布,支持右外连接和全外连接

SQLite 3.39.0 release supports right external connection and all external connection

HDL design peripheral tools to reduce errors and help you take off!

Registration opportunity of autowiredannotationbeanpostprocessor in XML development mode
随机推荐
AcWing 1127. Sweet butter solution (shortest path SPFA)
中缀表达式转换为后缀表达式(C语言代码+详解)
upload-labs
NMF-matlab
Build a master-slave mode cluster redis
MySQL
KT148A语音芯片ic的用户端自己更换语音的方法,上位机
HDL design peripheral tools to reduce errors and help you take off!
Educational codeforces round 129 (rated for Div. 2) supplementary problem solution
API文档工具knife4j使用详解
What are the benefits of multi terminal applet development? Covering Baidu applet, Tiktok applet, wechat applet development, and seizing the multi platform traffic dividend
rxjs Observable 自定义 Operator 的开发技巧
字典
KS004 基于SSH通讯录系统设计与实现
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
有时候只查询一行语句,执行也慢
Detailed tutorial on installing stand-alone redis
AcWing 1128. Messenger solution (shortest path Floyd)
c语言链表--待补充
GCC: Graph Contrastive Coding for Graph Neural NetworkPre-Training