Merge Two Sorted Lists

EasyLinked ListFree to try
Asked byAmazonMicrosoftFacebookAdobeOracleUber

Problem

Given the heads of two sorted linked lists, merge them into a single sorted list by splicing together the nodes of the two lists, and return the head of the merged list.

Examples

Example 1
Input:list1 = [1,2,4], list2 = [1,3,4]
Output:[1,1,2,3,4,4]
Example 2
Input:list1 = [], list2 = []
Output:[]

Constraints

  • The number of nodes in both lists is in the range [0, 50].

Try it now — no sign-up needed. Write your solution in Python or JavaScript, run it against test cases, and submit for a verdict, right in your browser.

Open the editor →

How to approach it: the Linked List pattern

A chain of nodes, each pointing to the next. No random access like an array, but insertion and deletion at a known position is O(1), and many classic problems come down to careful pointer rewiring.

Look for this pattern when

  • The problem explicitly gives you a linked list (reverse it, merge lists, detect a cycle, reorder it).
  • You need O(1) insertion/deletion without shifting elements, unlike an array.

Read the full Linked List guide →

Video walkthroughs

Original problem on LeetCode ↗

More Linked List problems