2Brute2Best
🔥0

Merge In Between Linked Lists

Medium·LeetCode #1669 Pro OnlySlight Variation
LeetCode
01

Problem Statement

You get two linked lists, list1 and list2. Positions in list1 start at 0.

Remove the nodes of list1 from position a to position b, both ends included. Then put all of list2 in their place. Afterwards, list2 sits between the node at position a − 1 and the node that was at position b + 1. Return the head of list1.

Input

list1 and list2, plus two positions a and b (with a ≤ b) that refer to list1.

Output

The head of list1 after the nodes from a to b are replaced by list2.

Example 1
Input: list1 = [0, 1, 2, 3, 4, 5], a = 3, b = 4, list2 = [1000000, 1000001, 1000002, 1000003, 1000004]
Output: [0, 1, 2, 1000000, 1000001, 1000002, 1000003, 1000004, 5]

Positions 3 and 4 (values 3 and 4) are removed. list2 goes in their place, and the 5 stays at the end.

Example 2
Input: list1 = [0, 1, 2, 3, 4, 5, 6], a = 2, b = 5, list2 = [1000000, 1000001, 1000002, 1000003, 1000004]
Output: [0, 1, 1000000, 1000001, 1000002, 1000003, 1000004, 6]

Positions 2 to 5 (values 2, 3, 4, 5) are removed. The 0 and the 1 stay before list2, and the 6 stays after it.

Constraints
  • • list1 has between 3 and 10^4 nodes.
  • • list2 has between 1 and 10^4 nodes.
  • • 1 ≤ a ≤ b < n − 1, where n is the number of nodes in list1. So there is always a node before the gap and a node after it.
  • • The node values are between 0 and 10^6.
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“replace positions a to b”
→ A block in the middle is removed and another list goes there → find node a − 1 and node b, then fix only the two ends.
“list2 goes in as a whole”
→ Splice the chain: node a − 1 points at list2's head, and list2's tail points at node b + 1. No values are compared.
“singly linked list”
→ Save the link to node b + 1 in a variable before changing any `next`.
03

Approach Complexity Ladder

Brute Force — collect the nodes in arrays, then relinkO(N + M)O(N + M)

Put the nodes of both lists in vectors, build the new order, relink

Optimal — walk to the gap, splice list2 in, reconnect the tailO(N + M)O(1)

Walk to node a − 1 and node b, splice list2 in, link its tail to node b + 1

Brute Force Intuition

Changing links in the middle of a list is easy to get wrong. A vector makes the new order obvious. Put the nodes of list1 and of list2 into two vectors. The new order is the first a nodes of list1, then all of list2, then the nodes after position b. Link each node to the next one in that order.

Recognition rule for this sub-pattern:

Find the node before the range, reverse exactly the range, then stitch both ends back.
Remember this · Linked List — Reversal
Trigger
Reverse whole / part / every k nodes, reorder, palindrome check, numbers stored as lists.
Think
Which piece do I reverse, and which nodes must I remember to reconnect afterwards?
Key question
Before reversing, who is the node just before the piece and who is just after?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Linked List — Reversal translate into production architectures and low-level memory mechanics.