Data StructuresEasy

Traversal

Traversal

Traversal means visiting every node in the list exactly once, starting from the head and following next pointers until NULL is reached.

The Core Pattern

Node* temp = head;       // start at head
while (temp != NULL) {   // loop until end
    // process temp->data
    temp = temp->next;   // move forward
}

What can you do during traversal?

GoalAction per node
Print all valuesprintf("%d", temp->data)
Count nodescount++
Find max valueif (temp->data > max) max = temp->data
Search for a valueif (temp->data == key) return temp
Sum all valuessum += temp->data

Time & Space

  • Time: O(N) — must visit all N nodes
  • Space: O(1) — only uses one extra pointer (temp), regardless of list size

The temp pointer is just a cursor. Moving temp does NOT modify the original list.