当前位置:网站首页>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)
边栏推荐
- 安装gstreamer开发依赖库到项目sysroot目录
- Difficulty comparison between high concurrency and multithreading (easy to confuse)
- 03-SDRAM: Write operation (burst)
- 【面试:并发篇38:多线程:线程池】ThreadPoolExecutor类的基本概念
- MySQL的触发器
- 文件 - 05 下载文件:根据文件Id下载文件
- Analysis of the principle and implementation of waterfall flow layout
- [PSQL] SQL基础教程读书笔记(Chapter1-4)
- 【Star项目】小帽飞机大战(八)
- 2022.07.22_每日一题
猜你喜欢

Redux state management

【Go语言刷题篇】Go完结篇函数、结构体、接口、错误入门学习

小实战项目之——吃货联盟订餐系统

Explain the example + detail the difference between @Resource and @Autowired annotations (the most complete in the entire network)

浅层了解欧拉函数

【云原生】3.3 Kubernetes 中间件部署实战

那些破釜沉舟入局Web3.0的互联网精英都怎么样了?

文件 - 05 下载文件:根据文件Id下载文件

【云原生】-Docker安装部署分布式数据库 OceanBase

【解决】npm ERR A complete log of this run can be found in npm ERR
随机推荐
【解决】npm ERR A complete log of this run can be found in npm ERR
毫米波技术基础
《白帽子说Web安全》思维导图
Zero-Shot Learning & Domain-aware Visual Bias Eliminating for Generalized Zero-Shot Learning
【第四章】详解Feign的实现原理
知识、创新、回报。
【微服务】(十六)—— 分布式事务Seata
(border-box) The difference between box model w3c and IE
Third-party library-store
Gradle剔除依赖演示
拓扑排序的两种方法 --- dfs+Kahn
【云原生】-Docker容器迁移Oracle到MySQL
Chapter 16: Constructing the Magic Square for Prime Numbers of Order n(5,7)
【愚公系列】2022年07月 Go教学课程 022-Go容器之字典
nohup原理
二叉树的还原(反序列化)
【Go语言入门】一文搞懂Go语言的最新依赖管理:go mod的使用
How to choose a suitable UI component library in uni-app
芯塔电子斩获第十一届中国双创大赛芜湖赛区桂冠
Redux state management