Traversal in a singly linked list means visiting each node from the head (first node) to the last node, one-by-one using the next pointer.
This operation is commonly used when searching, printing, or performing any action on each element in the list.
Suppose we have the following linked list:
current and assign it to the head of the list. This means current now points to the first node which has value 5.current is not null. It points to node with value 5.print(current.data) or perform any other logic using its value.
current = current.next. Now current points to node with value 15.current points to node with value 15.15.
current = current.next. Now current points to node with value 25.current points to node with value 25.25.
current = current.next. Now current points to node with value 35.current points to node with value 35.35.
current = current.next. Now current becomes null.current is now null, which means we’ve reached the end of the list.This process ensures that every node is visited exactly once, following the direction of the next pointers until the list ends.
This pseudocode shows how to traverse a singly linked list and print each node’s value.
function traverseList(head):
curr = head
while curr is not null:
print(curr.value)
curr = curr.next
Let’s walk through each part of the code to understand how the list is traversed.
function traverseList(head):
curr = head
Explanation: We define a function that takes the head of the list and uses a pointer curr to keep track of the current node.
while curr is not null:
print(curr.value)
curr = curr.next
Explanation: We loop until we reach the end of the list. For each node, we print its value and then move to the next node. The traversal stops when curr becomes null.
The following examples demonstrate how to traverse a singly linked list from head to tail and visit each node.
| Linked List | Traversal Output | Explanation |
|---|---|---|
| [10, 20, 30]
|
10 → 20 → 30 | Start from the head (10) and visit each node one by one by following the next pointer, until reaching the end of the list. |
| [5] | 5 | Only one node in the list. The traversal starts and ends at this single node. |
| [] | (empty) | Empty list. Since there are no nodes, traversal yields no output. |
| [7, 14, 21, 28]
|
7 → 14 → 21 → 28 | Traversal proceeds sequentially through all nodes in the list, maintaining order. |
| Traversal Case | Time Complexity | Explanation |
|---|---|---|
| Traverse Entire List | O(n) | We need to visit each node exactly once starting from the head until we reach the end (null). |
| Access Node at Index i | O(i) | We start from the head and move i steps forward. No direct access like arrays. |
| Search for Value | O(n) | We may have to visit each node once to check for the target value in the worst case. |
| Traversal Case | Space Complexity | Explanation |
|---|---|---|
| Iterative Traversal | O(1) | Only a pointer (or variable) is used to walk through the list. No extra space is needed. |
| Recursive Traversal | O(n) | Each recursive call adds a new stack frame, so space usage grows linearly with list size. |
next pointers until you reach null.i times from head to reach the node at index i.next node. Be mindful of call stack usage.