LeetCode 1669合并两个链表-中等
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                LeetCode 1669合并两个链表-中等
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.                        
                                給你兩個鏈表 list1 和 list2 ,它們包含的元素分別為 n 個和 m 個。
請你將 list1 中第 a 個節(jié)點到第 b 個節(jié)點刪除,并將list2 接在被刪除節(jié)點的位置。
下圖中藍色邊和節(jié)點展示了操作后的結(jié)果:
請你返回結(jié)果鏈表的頭指針。
輸入:list1 = [0,1,2,3,4,5], a = 3, b = 4, list2 = [1000000,1000001,1000002]
 輸出:[0,1,2,1000000,1000001,1000002,5]
 解釋:我們刪除 list1 中第三和第四個節(jié)點,并將 list2 接在該位置。上圖中藍色的邊和節(jié)點為答案鏈表。
輸入: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]
 解釋:上圖中藍色的邊和節(jié)點為答案鏈表。
提示:
3 <= list1.length <= 10^4 1 <= a <= b < list1.length - 1 1 <= list2.length <= 10^4代碼如下:
/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode() : val(0), next(nullptr) {}* ListNode(int x) : val(x), next(nullptr) {}* ListNode(int x, ListNode *next) : val(x), next(next) {}* };*/ class Solution { public:ListNode* mergeInBetween(ListNode* list1, int a, int b, ListNode* list2) {ListNode*endidx = list2;while(endidx->next) endidx = endidx->next;ListNode * pre = list1;int cnt = 1;ListNode *cur = list1->next;while(cnt <= b){if (cnt==a)pre->next = list2;pre = cur;cur = cur->next;cnt++;}endidx->next = cur;pre->next = nullptr;return list1;} };總結(jié)
以上是生活随笔為你收集整理的LeetCode 1669合并两个链表-中等的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 蔚来秦力洪:今年 NIO Day 会有重
- 下一篇: 三星为F1赛车打造全球首个大型LED标牌
