0
0
DSA Pythonprogramming~20 mins

Traversal and Printing a Linked List in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Linked List Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Traversing a Linked List
What is the printed output of the linked list after running this traversal code?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

head = Node(1)
head.next = Node(2)
head.next.next = Node(3)

current = head
while current:
    print(current.data, end=' -> ')
    current = current.next
print('null')
A1 -> 2 -> 3 -> null
Bnull
C3 -> 2 -> 1 -> null
D1 -> 2 -> null
Attempts:
2 left
💡 Hint
Think about how the traversal moves from the head node to the end.
Predict Output
intermediate
1:30remaining
What happens if the list is empty?
What is the output when the linked list is empty (head is None) and the same traversal code runs?
DSA Python
head = None
current = head
while current:
    print(current.data, end=' -> ')
    current = current.next
print('null')
Anull
B0 -> null
CNone -> null
DError: AttributeError
Attempts:
2 left
💡 Hint
If head is None, does the loop run?
🔧 Debug
advanced
2:00remaining
Identify the error in this linked list traversal code
What error does this code produce when run?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

head = Node(1)
head.next = Node(2)

current = head
while current.next:
    print(current.data, end=' -> ')
    current = current.next
print('null')
APrints '1 -> 2 -> null'
BPrints '1 -> null'
CPrints '1 -> 2' without 'null'
DPrints '1 -> null' and misses last node data
Attempts:
2 left
💡 Hint
Check the loop condition and what nodes get printed.
🧠 Conceptual
advanced
1:30remaining
How many nodes are printed by this traversal?
Given a linked list with 5 nodes, what is the number of nodes printed by this code?
DSA Python
current = head
count = 0
while current:
    print(current.data, end=' -> ')
    current = current.next
    count += 1
print('null')
print(count)
A6
B5
C4
D0
Attempts:
2 left
💡 Hint
The loop runs once per node until current is None.
🚀 Application
expert
2:30remaining
What is the output after reversing and printing the linked list?
What is the printed output after reversing this linked list and then printing it?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

def reverse(head):
    prev = None
    current = head
    while current:
        next_node = current.next
        current.next = prev
        prev = current
        current = next_node
    return prev

head = Node(1)
head.next = Node(2)
head.next.next = Node(3)

head = reverse(head)
current = head
while current:
    print(current.data, end=' -> ')
    current = current.next
print('null')
Anull
B1 -> 2 -> 3 -> null
C3 -> 2 -> 1 -> null
DError: infinite loop
Attempts:
2 left
💡 Hint
Reversing changes the direction of links between nodes.