Concept Flow - Traversal and Printing a Linked List
Start at head node
↓
Is current node None?
Yes→Stop traversal
No↓
Print current node data
↓
Move to next node
↩Back to check current node
Start from the first node, print its data, move to the next node, repeat until no nodes left.
Execution Sample
DSA Python
class Node:
def __init__(self, data):
self.data = data
self.next = None
current = head
while current:
print(current.data)
current = current.next
This code prints all data values in the linked list by moving from head to the end.
Execution Table
Step
Operation
Nodes in List
Pointer Changes
Visual State
1
Start at head
1 -> 2 -> 3 -> None
current = head (Node 1)
current -> 1 -> 2 -> 3 -> None
2
Print current.data
1 -> 2 -> 3 -> None
No change
Print: 1
current -> 1 -> 2 -> 3 -> None
3
Move to next node
1 -> 2 -> 3 -> None
current = current.next (Node 2)
current -> 2 -> 3 -> None
4
Print current.data
1 -> 2 -> 3 -> None
No change
Print: 2
current -> 2 -> 3 -> None
5
Move to next node
1 -> 2 -> 3 -> None
current = current.next (Node 3)
current -> 3 -> None
6
Print current.data
1 -> 2 -> 3 -> None
No change
Print: 3
current -> 3 -> None
7
Move to next node
1 -> 2 -> 3 -> None
current = current.next (None)
current -> None
8
Check current is None
1 -> 2 -> 3 -> None
No change
Traversal ends
💡 current becomes None, traversal stops
Variable Tracker
Variable
Start
After Step 1
After Step 3
After Step 5
After Step 7
Final
current
head (Node 1)
Node 1
Node 2
Node 3
None
None
Key Moments - 3 Insights
Why do we check if current is None before printing?
Because when current is None, it means we reached the end of the list and there is no data to print. See step 8 in execution_table where traversal stops.
Why do we move current to current.next after printing?
To go to the next node in the list. If we don't move, we would print the same node repeatedly. See steps 3, 5, and 7 where current moves forward.
What happens if the list is empty (head is None)?
The loop never runs because current starts as None. So nothing is printed and traversal ends immediately, like step 8 but from the start.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of current after step 5?
ANone
BNode 2
CNode 3
DNode 1
💡 Hint
Check the 'Pointer Changes' column at step 5 in execution_table.
At which step does the traversal stop because current becomes None?
AStep 6
BStep 8
CStep 7
DStep 5
💡 Hint
Look at the 'Operation' and 'Visual State' columns in execution_table for when current is None.
If the linked list had only one node, how many times would the print operation run?
A1 time
B0 times
C2 times
D3 times
💡 Hint
Refer to variable_tracker and execution_table to see how many nodes are printed.
Concept Snapshot
Traversal and Printing a Linked List:
- Start at head node
- While current node is not None:
- Print current node's data
- Move to next node
- Stop when current is None
This prints all nodes in order.
Full Transcript
Traversal and printing a linked list means starting at the first node called head. We check if the current node exists. If yes, we print its data and move to the next node. We repeat this until we reach the end where current is None. If the list is empty, nothing prints. This process ensures all nodes are visited and their data shown in order.