当前位置:网站首页>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)
边栏推荐
- Analysis of the implementation principle and detailed knowledge of v-model syntactic sugar and how to make the components you develop support v-model
- Conditional statements of shell (test, if, case)
- Zabbix6.2惊喜发布!特别优化中大型环境部署的性能!
- 关于求反三角函数的三角函数值
- 2022.7.29 Array
- How to use repeating-linear-gradient
- 【并发编程】ReentrantLock的lock()方法源码分析
- Database Principles Homework 2 — JMU
- Titanic 预测问题
- 多进程全局变量失效、变量共享问题
猜你喜欢
嵌入式系统驱动初级【2】——内核模块下_参数和依赖
postgresql源码学习(33)—— 事务日志⑨ - 从insert记录看日志写入整体流程
mysql索引失效的常见9种原因详解
测试 思维导图
Explain the example + detail the difference between @Resource and @Autowired annotations (the most complete in the entire network)
批量翻译软件免费【2022最新版】
英语翻译软件-批量自动免费翻译软件支持三方接口翻译
03-SDRAM:写操作(突发)
LeetCode:952. 按公因数计算最大组件大小【欧拉筛 + 并查集】
解决win11/win10在登陆界面(解锁界面)点击获取每日壁纸无效的问题 - get Daily Lockscreen and Wallpaper - Win11/10的登录界面背景图片在哪里?
随机推荐
tidyverse笔记——tidyr包
浅层了解欧拉函数
gstreamer的caps event和new_segment event
In-depth analysis of z-index
nohup principle
【TA-霜狼_may-《百人计划》】美术2.3 硬表面基础
Zero-Shot Learning & Domain-aware Visual Bias Eliminating for Generalized Zero-Shot Learning
2022.07.22_每日一题
leetcode 406. Queue Reconstruction by Height 根据身高重建队列(中等)
【C语言项目合集】这十个入门必备练手项目,让C语言对你来说不再难学!
Project exercise - memorandum (add, delete, modify, check)
R——避免使用 col=0
2022.07.26_每日一题
快速傅里叶变换(FFT)
【愚公系列】2022年07月 Go教学课程 022-Go容器之字典
gstreamer's caps event and new_segment event
Moment.js common methods
简单谈谈Feign
【第四章】详解Feign的实现原理
postgresql源码学习(33)—— 事务日志⑨ - 从insert记录看日志写入整体流程