当前位置:网站首页>2022.07.12 _ a day
2022.07.12 _ a day
2022-07-31 07:40:00 【No. い】
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 Right part after element is removed
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)
边栏推荐
- Explain the example + detail the difference between @Resource and @Autowired annotations (the most complete in the entire network)
- Web浏览器工作流程解析
- 2022.07.20_每日一题
- 从 Google 离职,前Go 语言负责人跳槽小公司
- 04-SDRAM: Read Operation (Burst)
- 第十六章:构建n(5,7)阶素数幻方
- 文件 - 07 删除文件: 根据fileIds批量删除文件及文件信息
- Difficulty comparison between high concurrency and multithreading (easy to confuse)
- SCI写作指南
- 2022.07.14_每日一题
猜你喜欢

文件 - 03 下载文件:根据文件id获取下载链接

Zero-Shot Learning & Domain-aware Visual Bias Eliminating for Generalized Zero-Shot Learning

关于求反三角函数的三角函数值

Leetcode952. 按公因数计算最大组件大小

【科普向】5G核心网架构和关键技术

事务的传播机制

【微服务】Nacos集群搭建以及加载文件配置

One of the small practical projects - food alliance ordering system

基于交替迭代法的交直流混合系统潮流计算matlab程序iEEE9节点系统算例

How to use repeating-linear-gradient
随机推荐
LeetCode刷题——摆动序列#376#Medium
项目 - 如何根据最近30天、最近14天、最近7天、最近24小时、自定义时间范围查询MySQL中的数据?
tidyverse笔记——管道函数
【 TA - frost Wolf _may - "one hundred plan" 】 art 2.3 hard surface
DAY18:Xss 靶场通关手册
【编程题】【Scratch三级】2022.03 冬天下雪了
解决安装 Bun 之后出现 zsh compinit: insecure directories, run compaudit for list. Ignore insecure directorie
Obtaining server and client information
Foreign trade website optimization - foreign trade website optimization tutorial - foreign trade website optimization software
LeetCode:952. 按公因数计算最大组件大小【欧拉筛 + 并查集】
leetcode 406. Queue Reconstruction by Height
Difficulty comparison between high concurrency and multithreading (easy to confuse)
什么是半波整流器?半波整流器的使用方法
Some derivation formulas for machine learning backpropagation
postgresql源码学习(34)—— 事务日志⑩ - 全页写机制
文件 - 03 下载文件:根据文件id获取下载链接
04-SDRAM:读操作(突发)
Postgresql source code learning (34) - transaction log ⑩ - full page write mechanism
DirectExchange交换机简单入门demo
2022.07.29_每日一题