当前位置:网站首页>2022.07.12_每日一题
2022.07.12_每日一题
2022-07-31 06:07:00 【诺.い】
1669. 合并两个链表
题目描述
给你两个链表 list1
和 list2
,它们包含的元素分别为 n
个和 m
个。
请你将 list1
中下标从 a
到 b
的全部节点都删除,并将list2
接在被删除节点的位置。
下图中蓝色边和节点展示了操作后的结果:

请你返回结果链表的头指针。
示例 1:
输入:list1 = [0,1,2,3,4,5], a = 3, b = 4, list2 = [1000000,1000001,1000002] 输出:[0,1,2,1000000,1000001,1000002,5] 解释:我们删除 list1 中下标为 3 和 4 的两个节点,并将 list2 接在该位置。上图中蓝色的边和节点为答案链表。
示例 2:

输入:list1 = [0,1,2,3,4,5,6], a = 2, b = 5, list2 = [1000000,1000001,1000002,1000003,1000004] 输出:[0,1,1000000,1000001,1000002,1000003,1000004,6] 解释:上图中蓝色的边和节点为答案链表。
提示:
3 <= list1.length <= 104
1 <= a <= b < list1.length - 1
1 <= list2.length <= 104
- 链表
coding
//leetcode submit region begin(Prohibit modification and deletion)
/** * 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 mergeInBetween1(ListNode list1, int a, int b, ListNode list2) {
ListNode res = list1;
for (int i = 1; i < a; i++) {
list1 = list1.next;
}
// remove <=> 左边界
ListNode remove = list1;
for (int i = 0; i < b - a + 1; i++) {
remove = remove.next;
}
// remove <=> 右边界
// 左边界 -> list2
while (list2 != null) {
list1.next = list2;
list1 = list1.next;
list2 = list2.next;
}
// list2 结束 -> list1 删除元素后的右部分
list1.next = remove.next;
return res;
}
public ListNode mergeInBetween2(ListNode list1, int a, int b, ListNode list2) {
ListNode cur = list1;
ListNode left = list1;
ListNode right = list1;
// 记录左右边界
for (int i = 0; cur != null; i++) {
if (a - 1 == i) {
left = cur;
}
if (b + 1 == i) {
right = cur;
}
cur = cur.next;
}
left.next = list2;
while (list2.next != null) {
list2 = list2.next;
}
list2.next = right;
return list1;
}
}
//leetcode submit region end(Prohibit modification and deletion)
边栏推荐
- 【云原生】-Docker安装部署分布式数据库 OceanBase
- 快速傅里叶变换(FFT)
- 4-1-7 二叉树及其遍历 家谱处理 (30 分)
- 基于LSTM的诗词生成
- Database Principles Homework 2 — JMU
- Obtaining server and client information
- tidyverse笔记——tidyr包
- 【Star项目】小帽飞机大战(七)
- 【解决】mysql本地计算机上的MySQL服务启动后停止。某些服务在未由其他服务或程序使用时将自动停止
- Analysis of the implementation principle and detailed knowledge of v-model syntactic sugar and how to make the components you develop support v-model
猜你喜欢
DirectExchange交换机简单入门demo
Analysis of the principle and implementation of waterfall flow layout
【科普向】5G核心网架构和关键技术
tidyverse笔记——dplyr包
Leetcode952. 按公因数计算最大组件大小
浅层了解欧拉函数
Some derivation formulas for machine learning backpropagation
【Go语言入门】一文搞懂Go语言的最新依赖管理:go mod的使用
R——避免使用 col=0
Analysis of pseudo-classes and pseudo-elements
随机推荐
Difficulty comparison between high concurrency and multithreading (easy to confuse)
iOS大厂面试查漏补缺
【Go语言入门】一文搞懂Go语言的最新依赖管理:go mod的使用
2. (1) Chained storage of stack, operation of chain stack (illustration, comment, code)
PCB抄板
项目 - 如何根据最近30天、最近14天、最近7天、最近24小时、自定义时间范围查询MySQL中的数据?
Chapter 17: go back to find the entrance to the specified traverse, "ma bu" or horse stance just look greedy, no back to search traversal, "ma bu" or horse stance just look recursive search NXM board
Leetcode952. 按公因数计算最大组件大小
Run the NPM will pop up to ask "how are you going to open this file?"
【Go语言刷题篇】Go完结篇函数、结构体、接口、错误入门学习
浅层了解欧拉函数
Jobject 使用
mysql索引失效的常见9种原因详解
《白帽子说Web安全》思维导图
Detailed explanation of js prototype
DirectExchange交换机简单入门demo
How to use repeating-linear-gradient
Exam Questions Previous True Questions Wrong Bills [The Fourth Session] [Provincial Competition] [Group B]
03-SDRAM: Write operation (burst)
英语翻译软件-批量自动免费翻译软件支持三方接口翻译