0
0
DSA Pythonprogramming~10 mins

Traversal and Printing a Linked List in DSA Python - Execution Trace

Choose your learning style9 modes available
Concept Flow - Traversal and Printing a Linked List
Start at head node
Is current node None?
YesStop 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
StepOperationNodes in ListPointer ChangesVisual State
1Start at head1 -> 2 -> 3 -> Nonecurrent = head (Node 1)current -> 1 -> 2 -> 3 -> None
2Print current.data1 -> 2 -> 3 -> NoneNo changePrint: 1 current -> 1 -> 2 -> 3 -> None
3Move to next node1 -> 2 -> 3 -> Nonecurrent = current.next (Node 2)current -> 2 -> 3 -> None
4Print current.data1 -> 2 -> 3 -> NoneNo changePrint: 2 current -> 2 -> 3 -> None
5Move to next node1 -> 2 -> 3 -> Nonecurrent = current.next (Node 3)current -> 3 -> None
6Print current.data1 -> 2 -> 3 -> NoneNo changePrint: 3 current -> 3 -> None
7Move to next node1 -> 2 -> 3 -> Nonecurrent = current.next (None)current -> None
8Check current is None1 -> 2 -> 3 -> NoneNo changeTraversal ends
💡 current becomes None, traversal stops
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 5After Step 7Final
currenthead (Node 1)Node 1Node 2Node 3NoneNone
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.