Challenge - 5 Problems
Linked List Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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')
Attempts:
2 left
💡 Hint
Think about how the traversal moves from the head node to the end.
✗ Incorrect
The code starts at the head node and prints each node's data followed by '->'. It stops when current is None, then prints 'null'. So the output is '1 -> 2 -> 3 -> null'.
❓ Predict Output
intermediate1: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')
Attempts:
2 left
💡 Hint
If head is None, does the loop run?
✗ Incorrect
Since head is None, current is None, so the while loop does not run. Only 'null' is printed.
🔧 Debug
advanced2: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')
Attempts:
2 left
💡 Hint
Check the loop condition and what nodes get printed.
✗ Incorrect
The loop runs while current.next is not None, so it stops before the last node. The last node's data (2) is not printed, only '1 -> null' is printed.
🧠 Conceptual
advanced1: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)
Attempts:
2 left
💡 Hint
The loop runs once per node until current is None.
✗ Incorrect
The loop visits each of the 5 nodes exactly once, so count is 5.
🚀 Application
expert2: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')
Attempts:
2 left
💡 Hint
Reversing changes the direction of links between nodes.
✗ Incorrect
The reverse function flips the list order. Printing after reversal shows nodes in reverse order: 3, 2, 1, then null.