Challenge - 5 Problems
Linked List Pop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output after popping the last node from the linked list?
Given the linked list and the pop operation code below, what is the printed linked list after popping the last node?
DSA Python
class Node: def __init__(self, data): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None def append(self, data): new_node = Node(data) if not self.head: self.head = new_node return last = self.head while last.next: last = last.next last.next = new_node def pop(self): if not self.head: return None if not self.head.next: popped = self.head.data self.head = None return popped current = self.head while current.next.next: current = current.next popped = current.next.data current.next = None return popped def print_list(self): current = self.head result = [] while current: result.append(str(current.data)) current = current.next if result: print(' -> '.join(result) + ' -> null') else: print('null') ll = LinkedList() ll.append(10) ll.append(20) ll.append(30) ll.pop() ll.print_list()
Attempts:
2 left
💡 Hint
Think about what happens to the last node when you pop it from the list.
✗ Incorrect
The pop method removes the last node (30). After popping, the list contains 10 and 20 nodes only.
❓ Predict Output
intermediate2:00remaining
What is the output after popping from a single-node linked list?
Consider the linked list with one node and the pop operation below. What is printed after popping the only node?
DSA Python
class Node: def __init__(self, data): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None def append(self, data): new_node = Node(data) if not self.head: self.head = new_node return last = self.head while last.next: last = last.next last.next = new_node def pop(self): if not self.head: return None if not self.head.next: popped = self.head.data self.head = None return popped current = self.head while current.next.next: current = current.next popped = current.next.data current.next = None return popped def print_list(self): current = self.head result = [] while current: result.append(str(current.data)) current = current.next if result: print(' -> '.join(result) + ' -> null') else: print('null') ll = LinkedList() ll.append(100) ll.pop() ll.print_list()
Attempts:
2 left
💡 Hint
What happens to the head when you pop the only node?
✗ Incorrect
Popping the only node sets the head to None, so printing the list shows 'null' with no nodes.
❓ Predict Output
advanced2:00remaining
What is the output after popping twice from the linked list?
Given the linked list and pop method below, what is the printed linked list after popping twice?
DSA Python
class Node: def __init__(self, data): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None def append(self, data): new_node = Node(data) if not self.head: self.head = new_node return last = self.head while last.next: last = last.next last.next = new_node def pop(self): if not self.head: return None if not self.head.next: popped = self.head.data self.head = None return popped current = self.head while current.next.next: current = current.next popped = current.next.data current.next = None return popped def print_list(self): current = self.head result = [] while current: result.append(str(current.data)) current = current.next if result: print(' -> '.join(result) + ' -> null') else: print('null') ll = LinkedList() ll.append(1) ll.append(2) ll.append(3) ll.append(4) ll.pop() ll.pop() ll.print_list()
Attempts:
2 left
💡 Hint
Each pop removes the last node. Think about which nodes remain after two pops.
✗ Incorrect
Popping twice removes nodes 4 and 3. Remaining nodes are 1 and 2.
🔧 Debug
advanced2:00remaining
Why does this pop method cause an error on a single-node list?
Examine the pop method below. It causes an error when popping from a list with only one node. What is the cause?
DSA Python
def pop(self): if not self.head: return None current = self.head while current.next.next: current = current.next popped = current.next.data current.next = None return popped
Attempts:
2 left
💡 Hint
Check what happens when current.next.next is accessed on a single-node list.
✗ Incorrect
When the list has two nodes, current is head. current.next.next is None, so the while loop condition fails immediately. But if the list has only one node, current.next is None, so current.next.next raises AttributeError. The code misses the single-node case.
🧠 Conceptual
expert2:00remaining
What is the time complexity of popping the last node in a singly linked list without tail pointer?
Consider a singly linked list with no tail pointer. What is the time complexity of the pop operation that removes the last node?
Attempts:
2 left
💡 Hint
Think about how you find the node before the last in a singly linked list.
✗ Incorrect
Without a tail pointer, to pop the last node, you must traverse from head to find the second last node, which takes O(n) time.