Skip to main content
← Problem catalogue Understand the pattern. Try it yourself. Review the solution. Practice this pattern →
Easy Linked List High frequency Solution Available Practice Mode

Merge Two Sorted Lists LeetCode Solution

Tags: #Linked List #Recursion

Approach Summary

Use a dummy head node. Compare heads of both lists, advance the smaller one. Attach remaining list at the end.

Practice Mode 2 Progressive Approaches

Solve it yourself before revealing the code

DSA interviews test reasoning from brute force to optimal. Think through the constraints first.

Algorithmic Pattern

Master the Linked List Pattern

View Pattern Guide →

Study the core invariant, step-by-step decision framework, and common interview traps for the Linked List pattern.

Framework: Linked List • Interview Prep: Tested at FAANG • Explore all Linked List problems & invariants →

How to Recognize This Pattern

  • Merge two sorted sequences
  • Dummy head pattern

Complexity Analysis

Time Complexity

O(n + m)

Space Complexity

O(1)

Tags

Linked List Recursion

Frequently Asked Questions

What is the optimal time and space complexity for Merge Two Sorted Lists?
The optimal solution for Merge Two Sorted Lists achieves O(n + m) runtime complexity and requires O(1) auxiliary space. Time complexity is O(m + n) where m and n are lengths of the lists because each node is attached to the merged list once. Space complexity is O(1) for the iterative pointer splice and O(m + n) for the recursive call stack.
How do I identify that Merge Two Sorted Lists requires the Linked List pattern?
In technical interviews, look for key problem signals: Merge two sorted sequences; Dummy head pattern. Recognizing these invariants allows you to bypass naive brute-force exhaustive search in favor of Linked List strategy.
What critical edge cases must be handled in Merge Two Sorted Lists?
Essential edge cases to verify include: Both lists are empty (returns null); One list is empty (returns the other list); Lists of different lengths (e.g. length 1 and length 10); Lists with duplicate or identical values. Always walk through these scenarios with your interviewer before coding.
Which tech companies frequently ask Merge Two Sorted Lists?
Merge Two Sorted Lists is a Easy-difficulty question tagged with high recurrence. It is actively asked in technical screening and onsite interview loops at Amazon, Microsoft, Apple.
Buy me a coffee
Daily Practice
0/0