0
0
DSA Pythonprogramming

Traversal and Printing a Linked List in DSA Python

Choose your learning style9 modes available
Mental Model
We move step-by-step through each connected item in the list to see or print its value.
Analogy: Imagine walking down a line of people holding hands, looking at each person's face one by one.
head -> [1] -> [2] -> [3] -> null
          ↑
Dry Run Walkthrough
Input: list: 1 -> 2 -> 3, print all values
Goal: Print all values in the list from start to end in order
Step 1: Start at the head node with value 1
head -> [1 ↑] -> [2] -> [3] -> null
Why: We begin printing from the first node
Step 2: Print value 1 and move to next node
head -> [1] -> [2 ↑] -> [3] -> null
Why: After printing current node, move forward to continue
Step 3: Print value 2 and move to next node
head -> [1] -> [2] -> [3 ↑] -> null
Why: Keep printing each node until we reach the end
Step 4: Print value 3 and move to next node (null)
head -> [1] -> [2] -> [3] -> null ↑
Why: Last node printed, next is null which ends traversal
Result:
1 -> 2 -> 3 -> null
Annotated Code
DSA Python
class Node:
    def __init__(self, value):
        self.value = value
        self.next = None

class LinkedList:
    def __init__(self):
        self.head = None

    def append(self, value):
        new_node = Node(value)
        if not self.head:
            self.head = new_node
            return
        curr = self.head
        while curr.next:
            curr = curr.next
        curr.next = new_node

    def print_list(self):
        curr = self.head
        while curr:
            print(curr.value, end=' -> ')
            curr = curr.next
        print('null')

# Driver code
ll = LinkedList()
ll.append(1)
ll.append(2)
ll.append(3)
ll.print_list()
curr = self.head
start traversal from the first node
while curr:
continue until we reach the end (null)
print(curr.value, end=' -> ')
print current node's value followed by arrow
curr = curr.next
move to next node to continue traversal
OutputSuccess
1 -> 2 -> 3 -> null
Complexity Analysis
Time: O(n) because we visit each of the n nodes exactly once
Space: O(1) because we only use a few variables regardless of list size
vs Alternative: Compared to recursive printing which uses O(n) space on call stack, this iterative approach is more space efficient
Edge Cases
empty list
prints only 'null' since there are no nodes
DSA Python
while curr:
single node list
prints the single value followed by 'null'
DSA Python
while curr:
When to Use This Pattern
When you need to visit or display every item in a linked list, use traversal by moving from head to next until null.
Common Mistakes
Mistake: Forgetting to move to the next node inside the loop, causing an infinite loop
Fix: Add 'curr = curr.next' inside the while loop to advance traversal
Mistake: Printing 'null' inside the loop instead of after traversal ends
Fix: Print 'null' only after the loop finishes to show list end
Summary
It visits each node in the linked list from start to end and prints its value.
Use it when you want to see or process all elements in order in a linked list.
The key is to start at the head and keep moving to the next node until you reach null.