当前位置:网站首页>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)
边栏推荐
- Conditional statements of shell (test, if, case)
- Analysis of pseudo-classes and pseudo-elements
- nohup原理
- 2022.7.29 数组
- 从入门到一位合格的爬虫师,这几点很重要
- 【面试:并发篇38:多线程:线程池】ThreadPoolExecutor类的基本概念
- SCI写作指南
- Zotero | Zotero translator plugin update | Solve the problem that Baidu academic literature cannot be obtained
- 安装gstreamer开发依赖库到项目sysroot目录
- Log4net 思维导图
猜你喜欢

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

Core Tower Electronics won the championship in the Wuhu Division of the 11th China Innovation and Entrepreneurship Competition

One of the small practical projects - food alliance ordering system

LeetCode brush # 376 # Medium - swing sequence

LeetCode刷题——摆动序列#376#Medium

Analysis of the implementation principle and detailed knowledge of v-model syntactic sugar and how to make the components you develop support v-model

03-SDRAM:写操作(突发)

科普 | “大姨太”ETH 和 “小姨太”ETC的爱恨情仇

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

2022.07.20_每日一题
随机推荐
Install the gstreamer development dependency library to the project sysroot directory
解决win11/win10在登陆界面(解锁界面)点击获取每日壁纸无效的问题 - get Daily Lockscreen and Wallpaper - Win11/10的登录界面背景图片在哪里?
2022.07.20_每日一题
【TA-霜狼_may-《百人计划》】美术2.3 硬表面基础
事务的传播机制
gstreamer的caps event和new_segment event
2022.07.13_每日一题
【Go】Go 语言切片(Slice)
iOS大厂面试查漏补缺
【微服务】Nacos集群搭建以及加载文件配置
Explain the example + detail the difference between @Resource and @Autowired annotations (the most complete in the entire network)
完美指南|如何使用 ODBC 进行无代理 Oracle 数据库监控?
零样本学习&Domain-aware Visual Bias Eliminating for Generalized Zero-Shot Learning
服务器和客户端信息的获取
【Go报错】go go.mod file not found in current directory or any parent directory 错误解决
2022.07.29_每日一题
Run the NPM will pop up to ask "how are you going to open this file?"
讲解实例+详细介绍@Resource与@Autowired注解的区别(全网最全)
Bulk free text translation
360 push-360 push tool-360 batch push tool