Merge Two Sorted Lists

Easy

Topics
Linked ListRecursion

You are given the heads of two sorted linked lists l1 and l2. Splice them into one sorted list and return its head.

Example 1

l1124null
l2134null
merged112344null
A dummy head avoids special-casing the first node, and when one list runs out the other is attached wholesale.
Linked list values from head to tail.
Input:  l1 = [1,2,4], l2 = [1,3,4]
Output: [1,1,2,3,4,4]

Example 2

l1
empty list
l20null
merged0null
Input:  l1 = [], l2 = [0]
Output: [0]

Constraints

  • 0 <= list length <= 50
  • -100 <= Node.val <= 100
  • Both lists are sorted non-decreasing.
Editorial — intuition, approach, complexity and pitfalls

Intuition

Since both inputs are already sorted, a single merge pass (like the merge step of merge sort) suffices — always attach the smaller current node.

Approach

  1. Use a dummy head so you never special-case the first node.
  2. Compare the fronts of both lists, attach the smaller, and advance that list.
  3. When one list runs out, attach the remainder of the other.

Dry Run

l1 = 1 -> 2 -> 4      l2 = 1 -> 3 -> 4
`dummy` is a throwaway node so the first attachment needs no special case; the
real answer is dummy.next at the end.
  step 1   fronts 1 and 1   1 <= 1 true    take l1's 1    built 1
  step 2   fronts 2 and 1   2 <= 1 false   take l2's 1    built 1 1
  step 3   fronts 2 and 3   2 <= 3 true    take l1's 2    built 1 1 2
  step 4   fronts 4 and 3   4 <= 3 false   take l2's 3    built 1 1 2 3
  step 5   fronts 4 and 4   4 <= 4 true    take l1's 4    built 1 1 2 3 4
  l1 is now empty, so tail.next = l1 or l2 attaches l2's remaining 4 wholesale
answer 1 1 2 3 4 4
The last line is doing more than it looks. `tail.next = l1 or l2` is not a loop:
the leftover tail is already a correctly sorted chain, so one pointer assignment
adopts all of it. Drop that line and the output stops at 1 1 2 3 4.
The comparison is <= rather than < so that equal values are taken from l1 first.
That makes the merge stable, which does not matter for the returned values here
(both lists start with a 1, and either order prints the same) but does matter the
moment nodes carry a payload beyond val.
No nodes are allocated except dummy. The merge re-points the existing nodes,
which is where the O(1) space comes from.

Complexity

  • Time — O(n+m): each node from both lists is visited once.
  • Space — O(1): pointers only; the merge is in place.

Common Pitfalls

  • Remember to attach the leftover tail — one list is usually longer.

Full Solution — all languages

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next
class Solution:
    def mergeTwoLists(self, l1, l2):
        dummy=ListNode(); tail=dummy
        while l1 and l2:
            if l1.val<=l2.val:
                tail.next=l1; l1=l1.next
            else:
                tail.next=l2; l2=l2.next
            tail=tail.next
        tail.next=l1 or l2
        return dummy.next
SensAI
Beta
Tap the mic to stop voice mode