AlgoScope

Merge Two Sorted Lists

AlgorithmbeginnerLinked List Operations

Repeatedly take the smaller head and hang it on the merged tail; relink instead of copying.

Decision · step 2 of 8Merging Sorted Lists: Two lists, interleaving
1✓47239headtailpq1 < 2

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.

Open in the player →or start at step 2

What you will see

Two lists feed a growing third; the smaller head detaches and attaches to the result's tail.

How merging sorted lists works →

Cost

BestO(n + m)
AverageO(n + m)
WorstO(n + m)
SpaceO(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

Leads to

Topics that need this one first.

Taught by the same lesson

Merging Sorted Lists covers these too, in the same run.