Merge In Between Linked Lists - Problem
Merge In Between Linked Lists is a fascinating linked list manipulation problem that tests your pointer management skills.

You are given two linked lists: list1 with n nodes and list2 with m nodes. Your task is to perform a surgical replacement operation:

๐ŸŽฏ Goal: Remove nodes from position a to position b (inclusive) in list1, and insert the entire list2 in their place.

Example: If list1 = [0,1,2,3,4,5], list2 = [1000000,1000001,1000002], a = 3, and b = 4, you need to:
โ€ข Remove nodes at positions 3 and 4 (values 3 and 4)
โ€ข Insert all of list2 in their place
โ€ข Result: [0,1,2,1000000,1000001,1000002,5]

This problem simulates real-world scenarios like replacing a section of code, splicing video segments, or updating parts of a document while maintaining the overall structure.

Input & Output

example_1.py โ€” Basic Replacement
$ Input: list1 = [0,1,2,3,4,5], list2 = [1000000,1000001,1000002], a = 3, b = 4
โ€บ Output: [0,1,2,1000000,1000001,1000002,5]
๐Ÿ’ก Note: We remove nodes at positions 3 and 4 (values 3 and 4) and insert the entire list2 in their place. The nodes before position 3 and after position 4 remain connected.
example_2.py โ€” Replace Single Node
$ Input: list1 = [0,1,2,3,4,5,6], list2 = [1000000,1000001], a = 2, b = 2
โ€บ Output: [0,1,1000000,1000001,3,4,5,6]
๐Ÿ’ก Note: Only one node at position 2 (value 2) is removed and replaced with the two nodes from list2.
example_3.py โ€” Replace to End
$ Input: list1 = [0,1,2,3], list2 = [100,101,102], a = 2, b = 3
โ€บ Output: [0,1,100,101,102]
๐Ÿ’ก Note: Remove the last two nodes (positions 2 and 3) and replace them with list2. Since there are no nodes after position 3, list2 becomes the new tail.

Constraints

  • The number of nodes in list1 is in the range [3, 104]
  • The number of nodes in list2 is in the range [1, 104]
  • 1 โ‰ค a โ‰ค b < n - 1 where n is the number of nodes in list1
  • 1 โ‰ค Node.val โ‰ค 106
  • Important: You are guaranteed that a and b are valid positions

Visualization

Tap to expand
๐Ÿ”— Linked List Surgery VisualizationBefore Surgery:list1: Remove positions 2-3, insert list201234keepanchorcutcutreconnectlist2: Insert this sequence100200300headtailSurgery Steps:1. Find anchor (node before cut)โ€ข Navigate to position a-1 (node with value 1)2. Find reconnection pointโ€ข Navigate to position b+1 (node with value 4)3. Connect anchor โ†’ list2 headโ€ข anchor.next = list2 (value 100)4. Connect list2 tail โ†’ reconnectionโ€ข list2_tail.next = reconnection_pointAfter Surgery - Final Result:011002003004anchorinsertedsequencefrom list2originalโšก Complexity Analysis:Time: O(n + m) - single pass through listsSpace: O(1) - only pointer variables usedOptimal: Direct pointer manipulation
Understanding the Visualization
1
Locate Cut Points
Find the node just before position a (our anchor point) and identify what comes after position b
2
Make the First Cut
Disconnect the chain at position a-1, saving the reference to where we'll reconnect later
3
Attach New Segment
Connect the anchor point to the head of list2, effectively grafting it into the original chain
4
Find Exit Point
Traverse to the end of list2 to find where we need to reconnect to the original chain
5
Complete the Surgery
Connect the tail of list2 to the node that was originally at position b+1
Key Takeaway
๐ŸŽฏ Key Insight: We only need to find two critical connection points and perform two pointer operations - no extra space needed!
Asked in
Amazon 42 Microsoft 35 Google 28 Meta 22
54.2K Views
Medium Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen