**输入:** 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
方法一:模拟
思路与算法
题目要求将 list}_1 的第 a 到 b 个节点都删除,将其替换为 list}_2。因此,我们首先找到 list}_1 中第 a - 1 个节点 preA,以及第 b + 1 个节点 aftB。由于 1 \le a \le b \lt n - 1(其中 n 是 list}_1 的长度),所以 preA 和 aftB 是一定存在的。
然后我们让 preA 的 next 指向 list}_2 的头节点,再让 list}_2 的尾节点的 next 指向 aftB 即可。
代码
[sol1-Python3]
1 2 3 4 5 6 7 8 9 10 11 12 13
classSolution: defmergeInBetween(self, list1: ListNode, a: int, b: int, list2: ListNode) -> ListNode: preA = list1 for _ inrange(a - 1): preA = preA.next preB = preA for _ inrange(b - a + 2): preB = preB.next preA.next = list2 while list2.next: list2 = list2.next list2.next = preB return list1
[sol1-C++]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classSolution { public: ListNode* mergeInBetween(ListNode* list1, int a, int b, ListNode* list2){ ListNode* preA = list1; for (int i = 0; i < a - 1; i++) { preA = preA->next; } ListNode* preB = preA; for (int i = 0; i < b - a + 2; i++) { preB = preB->next; } preA->next = list2; while (list2->next != nullptr) { list2 = list2->next; } list2->next = preB; return list1; } };
[sol1-Java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution { public ListNode mergeInBetween(ListNode list1, int a, int b, ListNode list2) { ListNodepreA= list1; for (inti=0; i < a - 1; i++) { preA = preA.next; } ListNodepreB= preA; for (inti=0; i < b - a + 2; i++) { preB = preB.next; } preA.next = list2; while (list2.next != null) { list2 = list2.next; } list2.next = preB; return list1; } }
[sol1-C#]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
publicclassSolution { public ListNode MergeInBetween(ListNode list1, int a, int b, ListNode list2) { ListNode preA = list1; for (int i = 0; i < a - 1; i++) { preA = preA.next; } ListNode preB = preA; for (int i = 0; i < b - a + 2; i++) { preB = preB.next; } preA.next = list2; while (list2.next != null) { list2 = list2.next; } list2.next = preB; return list1; } }
[sol1-C]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
struct ListNode* mergeInBetween(struct ListNode* list1, int a, int b, struct ListNode* list2) { structListNode* preA = list1; for (int i = 0; i < a - 1; i++) { preA = preA->next; } structListNode* preB = preA; for (int i = 0; i < b - a + 2; i++) { preB = preB->next; } preA->next = list2; while (list2->next != NULL) { list2 = list2->next; } list2->next = preB; return list1; }
[sol1-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
var mergeInBetween = function(list1, a, b, list2) { let preA = list1; for (let i = 0; i < a - 1; i++) { preA = preA.next; } let preB = preA; for (let i = 0; i < b - a + 2; i++) { preB = preB.next; } preA.next = list2; while (list2.next) { list2 = list2.next; } list2.next = preB; return list1; };
[sol1-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
funcmergeInBetween(list1 *ListNode, a int, b int, list2 *ListNode) *ListNode { preA := list1 for i := 0; i < a-1; i++ { preA = preA.Next } preB := preA for i := 0; i < b-a+2; i++ { preB = preB.Next } preA.Next = list2 for list2.Next != nil { list2 = list2.Next } list2.Next = preB return list1 }
复杂度分析
时间复杂度:O(n + m),其中 n 是 list}_1 的长度,m 是 list}_2 的长度。