当前位置:网站首页>力扣1669合并两个链表笔记
力扣1669合并两个链表笔记
2022-07-17 07:33:00 【雷霆小嘎巴】
给你两个链表 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]
解释:上图中蓝色的边和节点为答案链表。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/merge-in-between-linked-lists
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
题意是要在一个链表中删除一段并加入另一段
首先要知道删除的起始端和终点端
找到起始端和终点端后将起始端与另一段的起始端连接,再将终点端与另一段的终点端连接起来即可
class Solution { public ListNode mergeInBetween(ListNode list1, int a, int b, ListNode list2) { ListNode before = list1, after = list1; //before代表前半段,after代表后半段 for (int i = 1; i < a; i++) { before = before.next; } //从1到a-1遍历(一共a-1次)找到插入段的头部 for (int i = 0; i <= b; i++) { after = after.next; } //从0到b遍历(一共b+1次)找到插入段的尾部 ListNode begin = list2, end = list2; //begin代表list2的开始,end代表list2的结束 while (end.next != null) { end = end.next; } //找到插入段的尾部,即第一个链表后半段的头部 before.next = begin; end.next = after; //收尾依次相连 return list1; } }
在题解中找到了更优的方法,比较发现,这种方法好在第二次找b节点后面的节点时直接以第一次找的a-1节点开始,省去了重复的步骤,时间短,占用内存少
class Solution { public ListNode mergeInBetween(ListNode list1, int a, int b, ListNode list2) { ListNode cur = list1; int count = b - a + 2; // 找到list1中第a个节点的前一个节点 while ((a - 1) > 0) { a--; cur = cur.next; } ListNode left = cur; // 找到list1中第b个节点的后一个节点 while (count > 0) { cur = cur.next; count--; } ListNode right = cur; // 将list1中第a个节点的前一个节点指向list2的头节点 left.next = list2; // 找到list2的尾节点 while(list2.next != null) { list2 = list2.next; } // 将list2的尾节点指向list1中第b个节点的后一个节点 list2.next = right; return list1; } }
边栏推荐
- 3D laser slam:aloam --- interpretation of inter frame odometer code
- 没那么大的组合数
- DP dynamic planning enterprise level template analysis (Digital triangle, rising sequence, knapsack, state machine, compressed DP)
- Enjoy JVM -- knowledge about GC garbage collection
- Unity: window size adaptation when running on the browser after webgl Publishing
- Redis master-slave replication
- Redis介绍
- [characteristic Engineering]
- Viewing the technology stack of distributed system from the crash report of station B
- Great summary! Finally, someone explained all kinds of SQL join connections clearly
猜你喜欢

Error received from peer ipv4/Connection reset by peer Paddleserving服务化部署后报错

Interview question: outer margin folding problem (bug of block level elements in ordinary document flow)

Application of SCA on devsecops platform

Redis introduction

写代码遇到Qt相关问题

The core problem of concurrent programming

Real case: how to check the soaring usage of CPU after the system goes online?

1. Decision tree

Use of OpenCV polar transformation function warppolar
[characteristic Engineering]
随机推荐
Paddleserving服务化部署 tensorrt报错, shape of trt subgraph is [-1,-1,768],
Eureka基础知识
Csp-2020-6- role authorization
警惕!又一起网络钓鱼攻击事件:Uniswap被盗810万美元
5G正当时,无人驾驶未来将驶向何方?
1、flask基础
通过ip获取归属地
unity 自定义天空球模型防止被裁剪
凭借左程云(左神)的这份 “程序员代码面试指南”我入职了字节
Talk about distributed locks
《Flutter入门》flutter计算最近1个月、3个月、半年、12个月
总结的太好了!终于有人把SQL的各种连接Join都讲明白了
Redis common data types - redis list and redis set
TextView文字上下移动
Use of OpenCV polar transformation function warppolar
Deep learning 7 deep feedforward network
图片浏览器
With this "programmer code interview guide" from Zuo Chengyun (Zuo Shen), I joined byte
黑马程序员-软件测试-16阶段3-功能测试-175-198,URL组成介绍,请求内容以及组成说明行功能测试与数据库,url组成扩展说明,客户端与服务器请求与响应,-Fiddler按照以及功能检查确认,
Redis publishing and subscription