当前位置:网站首页>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 <= 1041 <= a <= b < list1.length - 11 <= 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)
边栏推荐
- shell之条件语句(test、if、case)
- Redux state management
- Postgresql source code learning (33) - transaction log ⑨ - see the overall process of log writing from the insert record
- Basic usage of Koa framework
- 第十六章:构建n(5,7)阶素数幻方
- 【解决】npm ERR A complete log of this run can be found in npm ERR
- 拓扑排序的两种方法 --- dfs+Kahn
- 文件 - 05 下载文件:根据文件Id下载文件
- 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
- Titanic 预测问题
猜你喜欢

2.(1)栈的链式存储、链栈的操作(图解、注释、代码)

R——避免使用 col=0

SCI写作指南

零样本学习&Domain-aware Visual Bias Eliminating for Generalized Zero-Shot Learning

双倍数据速率同步动态随机存储器(Double Data Rate Synchronous Dynamic Random Access Memory, DDR SDRAM)- 逻辑描述部分

搭建zabbix监控及邮件报警(超详细教学)

自动翻译软件-批量批量自动翻译软件推荐

Zotero | Zotero translator plugin update | Solve the problem that Baidu academic literature cannot be obtained

【微服务】Nacos集群搭建以及加载文件配置
![[PSQL] SQL基础教程读书笔记(Chapter1-4)](/img/76/d416f79b7f2c93c1c79a285c30d3e6.png)
[PSQL] SQL基础教程读书笔记(Chapter1-4)
随机推荐
高并发与多线程之间的难点对比(容易混淆)
毫米波技术基础
leetcode 406. Queue Reconstruction by Height
Exam Questions Previous True Questions Wrong Bills [The Fourth Session] [Provincial Competition] [Group B]
Some derivation formulas for machine learning backpropagation
Redux state management
Project exercise - memorandum (add, delete, modify, check)
What is float?What is document flow?Several ways and principles of clearing floats?What is BFC, how to trigger BFC, the role of BFC
Gradle剔除依赖演示
一文读懂 MongoDB 和 MySQL 的差异
安装gstreamer开发依赖库到项目sysroot目录
零样本学习&Domain-aware Visual Bias Eliminating for Generalized Zero-Shot Learning
opencv、pil和from torchvision.transforms的Resize, Compose, ToTensor, Normalize等差别
Run the NPM will pop up to ask "how are you going to open this file?"
gstreamer的caps event和new_segment event
codec2 BlockPool:不可读库
数据库概论 - MySQL的简单介绍
从 Google 离职,前Go 语言负责人跳槽小公司
Analysis of pseudo-classes and pseudo-elements
Install and use uView