← Two Pointers

Pattern Recognition Drill

#15 — Merge two sorted lists

Easy Linked Lists

The Problem

Merge two sorted linked lists into one sorted list.

What approach would you use?

Think about it before scrolling down.

Key Signals

Two Pointers

Compare heads of both lists, append the smaller to result. Dummy head simplifies the code. O(n+m).

Alt: DFS / Recursion

Recursive merge: pick smaller head, recurse on remaining. Clean code but O(n+m) stack space.

Common Trap

Don't forget to append the remaining list when one is exhausted.

← #14 #16 →