- 1Singly Linked List Introduction
- 2Singly Linked List – Create Operation
- 3Singly Linked List – Insert Operation
- 4Singly Linked List – Delete Operation
- 5Singly Linked List – Traversal Operation
- 6Singly Linked List – Search Operation
- 7Singly Linked List – Get Length Operation
- 8Singly Linked List – Reverse Operation
- 9Doubly Linked List Introduction
- 10Doubly Linked List – Create Operation
- 11Doubly Linked List – Insert Operation
- 12Doubly Linked List – Delete Operation
- 13Doubly Linked List – Traversal Operation
- 14Doubly Linked List – Search Operation
- 15Doubly Linked List – Get Length Operation
- 16Doubly Linked List – Reverse Operation
Singly Linked List – Traversal Operation
Traversal Operation in Singly Linked List
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.
Visual Example
Suppose we have the following linked list:
Detailed Step-by-Step Traversal
- Initialize a pointer: Create a pointer variable named
current
and assign it to thehead
of the list. This meanscurrent
now points to the first node which has value5
. - Step 1:
- Check if
current
is notnull
. It points to node with value5
. - Visit this node. For example, you can
print(current.data)
or perform any other logic using its value. - Move to the next node:
current = current.next
. Nowcurrent
points to node with value15
.
- Check if
- Step 2:
current
points to node with value15
.- Process the data — e.g., print
15
. - Move to next:
current = current.next
. Nowcurrent
points to node with value25
.
- Step 3:
current
points to node with value25
.- Process the data — e.g., print
25
. - Move to next:
current = current.next
. Nowcurrent
points to node with value35
.
- Step 4:
current
points to node with value35
.- Process the data — e.g., print
35
. - Move to next:
current = current.next
. Nowcurrent
becomesnull
.
- End of traversal:
current
is nownull
, which means we’ve reached the end of the list.- Exit the traversal loop.
This process ensures that every node is visited exactly once, following the direction of the next
pointers until the list ends.
Complete Pseudocode: Traverse a Singly Linked List
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
.
Singly Linked List Traversal Operation Examples
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. |
Time Complexity of Singly Linked List Traversal Operation
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. |
Space Complexity of Singly Linked List Traversal Operation
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. |
Key Concepts
- Traversal: Start from the head and follow
next
pointers until you reachnull
. - Index-based Access: Move forward
i
times from head to reach the node at indexi
. - Search Operation: Compare each node’s value until a match is found or end is reached.
- Recursive Traversal: Function calls itself with
next
node. Be mindful of call stack usage.
Comments
Loading comments...