Merge Two Sorted Lists
Repeatedly take the smaller head and hang it on the merged tail; relink instead of copying.
Heads: 1 in a, 2 in b. The smallest is 1 from a, so it is unhooked from its list and linked after nothing: it becomes the merged head.
What you will see
Two lists feed a growing third; the smaller head detaches and attaches to the result's tail.
Cost
| Best | O(n + m) |
|---|---|
| Average | O(n + m) |
| Worst | O(n + m) |
| Space | O(1) |
How you work with it here
play it through, step one change at a time, scrub to any step, run it on your own input, predict what happens next, try operations in any order.
Screen readers: Each node announces its value and what it points to; each step announces the pointer change ("node 3 now points to node 5").
Reduced motion: Pointer arrows redraw in place with a brief emphasis instead of animating the detach and reattach.
Before this
Taught by the same lesson
Merging Sorted Lists covers these too, in the same run.