#21

Merge Two Sorted Lists

Easy Dummy head merge

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Merge two sorted linked lists into one sorted list by splicing their nodes together.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: zip two sorted lists into one sorted list.

Why this works: the next node is always the smaller of the two heads; a dummy node holds the start.

Example: 1→2→4 and 1→3→4

1, 1, 2, 3, 4, 4

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Forgetting to attach the leftover list after the loop.

Solve on LeetCode ↗
Complexity
  • Time: O(n + m).
  • Space: O(1).
Topic: Linked List
🎬 Episodes 40–45: video coming soon