Step by step · Python, C++ and Java

Linked list traversal, visualized

A linked list is a chain of nodes, each holding a value and a reference to the next. There is no index — to reach anything, you follow the links.

How it works
class Node:
    def __init__(self, val):
        self.val = val
        self.next = None

head = Node(10)
head.next = Node(20)
head.next.next = Node(30)

curr = head
total = 0
while curr is not None:
    total += curr.val
    curr = curr.next

print(total)

How linked list traversal works

The examples build a three-node list by assigning each new node to the previous node’s next field. A pointer curr then starts at the head and loops until it reaches null, adding each value to a running total and stepping to curr.next.

In C++ the links are raw pointers to nodes on the heap; in Java they are object references. All three are drawn the same way, which is the point: the structure is the idea, not the syntax.

What to watch for when it runs

  1. Each node is a box and each next is an arrow; null is the end of the chain.
  2. curr is shown as a label on whichever node it points at, moving one link per step.
  3. Assigning head.next.next adds an arrow, not a copy — the nodes are shared.

Time and space complexity

Time
O(n) to traverse
Space
O(1) extra

Reaching the k-th node takes k steps, where an array takes one. Inserting after a node you already hold is O(1), where an array has to shift everything after it.

Stuck on your own version?

Paste it into the visualizer and watch your own code run, line by line — bugs included.