Data Structures · Visualization
Linked List: Traverse
Linked List
Traversing means visiting every node in the list, one at a time, starting from the head and following each 'next' pointer until reaching the end (null). Time: O(n). Space: O(1).
Big-O: Lookup O(n); insert/delete at head O(1).
Linked List: Traverse
Linked ListTraversing means visiting every node in the list, one at a time, starting from the head and following each 'next' pointer until reaching the end (null). It is the most basic operation because almost every other linked list algorithm is built on top of it.
How Traverse Works
- Start a pointer, current, at the head of the list.
- While current is not null, process (read/print) current's value.
- Move current forward: current = current.next.
- Stop when current becomes null — you've reached the end.
Traverse Worked Example
Input: head -> 5 -> 10 -> 15 -> null
- current = head (5) → visit 5
- current = current.next (10) → visit 10
- current = current.next (15) → visit 15
- current = current.next (null) → stop
Output: Visited in order: 5, 10, 15
Press ▶ Run to watch it step by step.
def traverse(head):
cur = head
while cur:
print(cur.val, end=" -> ")
cur = cur.next # follow next pointer
print("None")
# example: traverse on [5, 10, 15]Behind the Scenes Execution
Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.
Traverse Time & Space Complexity
When to Use Traverse
- Printing or processing every element in a list
- Searching, summing, or validating list contents
- The foundation for almost every other linked list operation
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Ready for the next step?
See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.