Skip to content
AI360Xpert
Beta
Difficulty: EasyLinked List

Merge Two Sorted Lists

Problem in Plain English

You are given the heads of two linked lists that are each sorted in non-decreasing order. Splice their nodes together into one sorted list and return its head.

Problem Statement

You are given the heads of two singly linked lists, list1 and list2, each sorted in non-decreasing order.

Merge the two lists by splicing their existing nodes into one sorted list, then return the head of the merged list.

Assume the two inputs are acyclic and do not share nodes. Reuse every input node exactly once; changing their next links is allowed, but replacing them with copies is not. Equal values may appear in either order; the snippets consistently take list1 first on ties.

Constraints

  • The number of nodes in each list is in the range [0, 50].
  • -100 <= Node.val <= 100
  • Both list1 and list2 are sorted in non-decreasing order.
  • The input lists are acyclic and have disjoint node identities; the merge may mutate their next links.

Examples

Example 1

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

Take 1 from list1, 1 from list2, 2 from list1, 3 from list2, and the two 4s. All six original nodes remain, including both occurrences of each repeated value, in non-decreasing order.

Example 2

Input
list1 = [], list2 = []
Output
[]

Neither input owns any nodes, so there is nothing to splice and the returned head is null.

Example 3

Input
list1 = [], list2 = [0]
Output
[0]

There is no competing head. Return the existing node with value 0 as the whole result, preserving its identity.

Example 4

Input
list1 = [-3,-1,2], list2 = [-3,0,2,2]
Output
[-3,-3,-1,0,2,2,2]

Negative values compare numerically, and equal values are not deduplicated. Both -3 nodes and all three 2 nodes are retained; exhausting list1 still leaves two nodes from list2 to attach.

Intuition

Each sorted list exposes its smallest remaining value at its head, so the smallest remaining value overall must be one of those two heads. In Example 1, choose the first 1, then the other 1, then 2 and 3, and finally both 4s. Keep an output tail to attach each chosen existing node; a temporary dummy node supplies a predecessor even before the first real node is chosen. Node identity matters: a newly allocated node with the same value is still a different node.

Approaches

Collect Nodes and Sort

Solution Details

Reveal Collect Nodes and Sort: intuition, complexity, and code

Hints

Hint 1
Picture merging two sorted piles by repeatedly taking the smaller visible card.
Hint 2
A dummy node before the merged list removes the special case for choosing its first node.
Hint 3
When one list runs out, the rest of the other list is already sorted, so attach it whole instead of looping node by node.

Edge Cases

  • Both lists are empty, so the loop never runs and dummy.next is null.
  • One list is empty, so the answer is the other list attached whole.
  • All values in one list are smaller, so the other list is spliced on in a single step after the loop.
  • Repeated values must retain every node; equality never means a duplicate node can be discarded.
  • Inputs must not share a tail: reusing overlapping nodes without a separate policy can introduce a cycle.

Common Mistakes and Interview Tips

  • Forgetting to attach the leftover nodes after the loop, which silently drops the tail of the longer list.
  • Returning dummy or tail instead of dummy.next, which includes the placeholder or only the last node.
  • Advancing tail without moving it to the appended node, which overwrites the same link every iteration.
  • Sorting values and allocating replacement nodes satisfies value equality but violates the original-node splicing requirement.
  • Using JavaScript's default sort compares text representations; compare node values numerically.
  • Forgetting to terminate the sorted-reference chain can leave an old link pointing backward and create a cycle.

Key Takeaway

Merge sorted linked lists by preserving a chosen-prefix invariant and comparing only exposed heads. A dummy node removes the empty-prefix special case; reference-based rewiring preserves ownership without allocating replacement data nodes.