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
- Each node is a box and each next is an arrow; null is the end of the chain.
- curr is shown as a label on whichever node it points at, moving one link per step.
- 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.