Pattern Recognition Drill
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.
Compare heads of both lists, append the smaller to result. Dummy head simplifies the code. O(n+m).
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.