15% off all plans & roadmaps Use code
See plansLeetCode 21 · Solution & editorial
You are given the heads of two sorted linked lists list1 and list2.
Merge Two Sorted Lists is a Linked List Manipulation Patterns problem, in the Merging Two Sorted Lists subpattern. The pattern covers in-place reversal, merging sorted lists, reordering, and intersection detection
Learn to merge two sorted linked lists efficiently. Foundation for merge sort and k-way merge problems.
The reference solution runs in O(n + m) time and O(n + m) space.
Merge Two Sorted Lists has been reported in interviews at Amazon, Adobe, Bloomberg, Capital One, Facebook, Microsoft, Oracle, LinkedIn.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Merge Two Sorted Lists is a Linked List Manipulation Patterns problem, in the Merging Two Sorted Lists subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.