Interview Question Merge two sorted singly linked lists without creating new nodes
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
This interview problem tests whether you can manipulate pointers cleanly rather than whether you can allocate memory. Because you are not allowed to create new nodes, the solution must reuse the existing nodes and relink them in sorted order.
The core idea: walk both lists and relink nodes
Suppose both input lists are already sorted in ascending order. At any point, the correct next node in the merged list is simply the smaller head node of the two current lists.
That means the algorithm is:
- choose the smaller head as the merged head
- keep a tail pointer to the last node in the merged list
- repeatedly attach the smaller current node from the two lists
- when one list ends, attach the remainder of the other list
No new nodes are required. You are only changing next references.
Python implementation without creating nodes
Here is a complete runnable example:
This solution is in-place with respect to node allocation. The nodes from the original lists become the nodes of the merged list.
Why this is O(n + m) and O(1) extra space
Each node is visited once. If the first list has n nodes and the second has m, the total work is O(n + m).
The extra space is O(1) because:
- no new nodes are created
- only a few pointer variables are used
That is exactly why this is a classic interview question. It checks whether you can produce the optimal linked-list merge instead of defaulting to array-style rebuilding.
A recursive version also reuses nodes
You can solve the same problem recursively:
This also avoids creating new nodes, but it uses call stack space. In interviews, the iterative version is often preferred because it keeps auxiliary space at O(1).
Example walk-through
Take:
The merge goes like this:
- choose
1as head - compare
4and2, attach2 - compare
4and3, attach3 - compare
4and8, attach4 - compare
7and8, attach7 - attach the remaining
8
Result:
The important detail is that these are the original nodes, only re-threaded in a new order.
Common Pitfalls
The biggest mistake is allocating a brand-new merged list, which violates the main constraint of the problem.
Another common issue is losing track of the next node before reassigning pointers. Pointer updates must happen in a safe order.
People also forget to handle empty input lists. Those base cases should be checked immediately.
Finally, once one list is exhausted, do not keep comparing against None. Just attach the remaining tail of the other list directly.
Summary
- The correct solution reuses existing nodes and only rewires
nextpointers. - Use a head pointer and a tail pointer to build the merged list in order.
- The iterative solution runs in
O(n + m)time andO(1)extra space. - A recursive solution also works but uses stack space.
- Handle empty lists and the final remaining tail carefully to avoid pointer bugs.

