Merge In Between Linked Lists — Problem & Solution
Merge In Between Linked Lists solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. A medium problem.
Merge In Between Linked Lists is a medium coding problem on two pointers, asked in Amazon interviews.
- Difficulty: Medium
- Topics: Two Pointers
- Asked at: Amazon
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
You are given two singly linked lists, list1 and list2, and two integers a <= b. Remove the nodes of list1 at positions a through b (0-indexed, inclusive) and put the whole of list2 in their place.
Return the head of the resulting list.
Example 1
Input: list1 = [10,1,13,6,9,5], a = 3, b = 4, list2 = [100,101,102]
Output: [10,1,13,100,101,102,5]
Explanation: Positions 3 and 4 (values 6 and 9) are replaced by list2.
Example 2
Input: list1 = [0,1,2,3,4,5,6], a = 2, b = 5, list2 = [90,91,92,93,94]
Output: [0,1,90,91,92,93,94,6]
Example 3
Input: list1 = [4,8,15], a = 1, b = 1, list2 = [16]
Output: [4,16,15]
Constraints
3 <= list1.length <= 10^41 <= a <= b < list1.length - 11 <= list2.length <= 10^40 <= Node.val <= 10^6
Hints for Merge In Between Linked Lists
Hint 1
Only two places in
list1matter: the node just before positionaand the node just after positionb.Hint 2
Walk
list1once to find both (positionsa - 1andb + 1); the constraints guarantee they exist.Hint 3
Find the tail of
list2, then linkbefore.next = list2andtail.next = after.
How to solve Merge In Between Linked Lists
Splicing a list in costs two link changes once the two boundary nodes are found: the node before the removed block points at list2, and list2's tail points at the node after the block.
Approach
- Walk
a - 1steps fromlist1tobefore. - Continue from
beforeanotherb - a + 2steps toafter, the node at positionb + 1. - Walk
list2to its last nodetail. - Set
before.next = list2,tail.next = after, and returnlist1.
Why it works
Positions a..b are exactly the nodes strictly between before and after; redirecting before to list2 and list2's tail to after removes them and inserts list2 in their place, leaving the rest of list1 linked as before. Since a >= 1, the head of list1 never changes.
Complexity
- Time —
O(n + m) - Space —
O(1)
Pitfalls
- Off by one:
beforeis at positiona - 1andafteratb + 1. - Forgetting to connect
list2's tail cuts off the rest oflist1. - Find
afterbefore changingbefore.next, or the removed block is no longer reachable frombefore.
Reference solution
Python
from typing import Optional
def mergeInBetween(list1: Optional[ListNode], a: int, b: int, list2: Optional[ListNode]) -> Optional[ListNode]:
before = list1
for _ in range(a - 1):
before = before.next
after = before
for _ in range(b - a + 2):
after = after.next
tail = list2
while tail.next:
tail = tail.next
before.next = list2
tail.next = after
return list1Java
public static ListNode mergeInBetween(ListNode list1, int a, int b, ListNode list2) {
ListNode before = list1;
for (int i = 0; i < a - 1; i++) before = before.next;
ListNode after = before;
for (int j = 0; j < b - a + 2; j++) after = after.next;
ListNode tail = list2;
while (tail.next != null) tail = tail.next;
before.next = list2;
tail.next = after;
return list1;
}C++
ListNode* mergeInBetween(ListNode* list1, int a, int b, ListNode* list2) {
ListNode* before = list1;
for (int i = 0; i < a - 1; i++) before = before->next;
ListNode* after = before;
for (int j = 0; j < b - a + 2; j++) after = after->next;
ListNode* tail = list2;
while (tail->next) tail = tail->next;
before->next = list2;
tail->next = after;
return list1;
}JavaScript
var mergeInBetween = function(list1, a, b, list2) {
var before = list1;
for (var i = 0; i < a - 1; i++) before = before.next;
var after = before;
for (var j = 0; j < b - a + 2; j++) after = after.next;
var tail = list2;
while (tail.next !== null) tail = tail.next;
before.next = list2;
tail.next = after;
return list1;
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 165 two pointers problems · the whole catalogue
Learn the technique: Two Pointers Technique