当前位置:网站首页>【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;
}
边栏推荐
- 中缀表达式转换为后缀表达式(C语言代码+详解)
- 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?
- R language uses econcharts package to create microeconomic or macroeconomic maps, and indifference function to visualize indifference curve
- KT148A语音芯片ic的硬件设计注意事项
- 勵志!大凉山小夥全獎直博!論文致謝看哭網友
- Conscience summary! Jupyter notebook from Xiaobai to master, the nanny tutorial is coming!
- Detailed explanation of VBScript (I)
- CRM客户关系管理系统
- B端电商-订单逆向流程
- AcWing 1126. Minimum cost solution (shortest path Dijkstra)
猜你喜欢

Registration opportunity of autowiredannotationbeanpostprocessor in XML development mode

Implementation of online shopping mall system based on SSM

Windows2008r2 installing php7.4.30 requires localsystem to start the application pool, otherwise 500 error fastcgi process exits unexpectedly

Overview of browser caching mechanism

自动生成VGG图像注释文件

Kt148a voice chip IC software reference code c language, first-line serial port

浏览器缓存机制概述

嵌入式(PLD) 系列,EPF10K50RC240-3N 可编程逻辑器件

Py's interpret: a detailed introduction to interpret, installation, and case application

JASMINER X4 1U深度拆解,揭开高效省电背后的秘密
随机推荐
AcWing 343. Sorting problem solution (Floyd property realizes transitive closure)
Codeforces Round #802 (Div. 2) 纯补题
ShardingSphere-JDBC5.1.2版本关于SELECT LAST_INSERT_ID()本人发现还是存在路由问题
函数高阶-柯里化实现
Introduction to program ape (XII) -- data storage
AcWing 340. 通信线路 题解(二分+双端队列BFS求最短路)
基于SSM实现网上购物商城系统
AcWing 1131. Saving Private Ryan (the shortest way)
450-深信服面经1
AcWing 1134. Shortest circuit counting problem solution (shortest circuit)
AcWing 1137. 选择最佳线路 题解(最短路)
Automatic reading of simple books
SQLite 3.39.0 release supports right external connection and all external connection
分享几个图床网址,便于大家分享图片
Chapter 7 - class foundation
Registration opportunity of autowiredannotationbeanpostprocessor in XML development mode
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?
Detailed tutorial on installing stand-alone redis
高并发下如何避免产生重复数据?
Educational Codeforces Round 129 (Rated for Div. 2) 补题题解