AlgoViz
← Back to trail

Linked List Traversal

Easy+60 XP

Follow next pointers from head to null — a pointer is a reference to a node.

No visualization loaded.

Watch

i

Press Run to begin.

A linked list is a chain. Each link (called a node) holds a value AND an arrow pointing to the next node. You hold onto the first node (the 'head') and follow the arrows one by one until an arrow points to nothing (null) — that's the end. Unlike an array's neat numbered row, the nodes can live anywhere; the arrows are what keep them in order.

What's a 'node', a 'pointer', and 'head'?

A node is one link in the chain: a little box holding a value plus an arrow. A pointer (or 'next') is that arrow — it says 'the next node is over there.' The head is your handle on the whole list: the very first node. Lose the head and you've lost the list, because it's the only way in.

What is 'null' and why does it matter?

null means 'nothing here — there is no next node.' The last node's arrow points to null, which is how you know you've reached the end. When you're walking the list, you stop the moment the current arrow is null. No null check = walking off the end of the chain.

If I can jump to arr[5] instantly, why use a linked list?

Trade-off. An array gives instant jump-to-index but inserting in the middle means shifting everything over. A linked list can't jump — to reach the 5th node you must follow 5 arrows (O(n)) — but inserting is cheap: just re-point a couple of arrows, no shifting. Arrays win for random access; linked lists win for lots of inserts/removes.

Why does reaching a node take O(n) here but O(1) in an array?

An array knows where every box is by math (start + index), so it leaps straight there. A linked list only knows where the NEXT node is, via the arrow — so to reach node 5 you genuinely have to visit 0,1,2,3,4 first. No shortcuts; you walk the chain.

🧠Linked list = nodes joined by arrows; start at head, follow next until null. No instant indexing (O(n) to reach a node), but cheap inserts.
📈

How the work grows

input size n →work ↑
Time
O(n) to reach a node
Space
O(n) — a node per value

O(n) to reach a node is fair — grows in step with the data. There's no jump-to-index: to reach the kth node you follow k arrows, so access grows with the list. (Inserting once you're there, though, is instant — the linked list's real strength.)

Faint dotted lines = O(1) (flat) and O(n) (straight) for comparison.

curr (current node)already visited

Practice problems