当前位置:网站首页>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)
边栏推荐
- Leetcode952. 按公因数计算最大组件大小
- Automatic translation software - batch batch automatic translation software recommendation
- 【解决】npm ERR A complete log of this run can be found in npm ERR
- 2. (1) Chained storage of stack, operation of chain stack (illustration, comment, code)
- QFileInfo常规方法
- 中断及pendSV
- postgresql源码学习(34)—— 事务日志⑩ - 全页写机制
- Some derivation formulas for machine learning backpropagation
- 2.(1)栈的链式存储、链栈的操作(图解、注释、代码)
- 剑指offer(一)
猜你喜欢

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

Log4net 思维导图

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

Obtaining server and client information

PCB抄板

Difficulty comparison between high concurrency and multithreading (easy to confuse)

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

03-SDRAM:写操作(突发)
解决win11/win10在登陆界面(解锁界面)点击获取每日壁纸无效的问题 - get Daily Lockscreen and Wallpaper - Win11/10的登录界面背景图片在哪里?

【解决】npm ERR A complete log of this run can be found in npm ERR
随机推荐
【解决】mysql本地计算机上的MySQL服务启动后停止。某些服务在未由其他服务或程序使用时将自动停止
第十七章:回溯探求指定入口的马步遍历,贪心无回溯探求马步遍历,递归探求nxm棋盘带障碍马步遍历
nohup原理
【微服务】(十六)—— 分布式事务Seata
gstreamer's caps event and new_segment event
小实战项目之——吃货联盟订餐系统
SCI写作指南
Kubernetes scheduling
van-uploader上传图片,使用base64回显无法预览的问题
链表实现及任务调度
DAY18:Xss 靶场通关手册
Moment.js common methods
CHI论文阅读(1)EmoGlass: an End-to-End AI-Enabled Wearable Platform for Enhancing Self-Awareness of Emoti
简单谈谈Feign
Zero-Shot Learning & Domain-aware Visual Bias Eliminating for Generalized Zero-Shot Learning
tidyverse笔记——管道函数
剑指offer(一)
项目 - 如何根据最近30天、最近14天、最近7天、最近24小时、自定义时间范围查询MySQL中的数据?
Difficulty comparison between high concurrency and multithreading (easy to confuse)
解决安装 Bun 之后出现 zsh compinit: insecure directories, run compaudit for list. Ignore insecure directorie