Challenge - 5 Problems
Linked List Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output after deleting node at position 2 in a linked list
What is the printed linked list after deleting the node at position 2 (0-based index)?
DSA Python
class Node: def __init__(self, data): self.data = data self.next = None def delete_node(head, position): if position == 0: return head.next current = head count = 0 while current is not None and count < position - 1: current = current.next count += 1 if current is None or current.next is None: return head current.next = current.next.next return head def print_list(head): current = head result = [] while current: result.append(str(current.data)) current = current.next print(' -> '.join(result) + ' -> null') # Create linked list 10 -> 20 -> 30 -> 40 -> null head = Node(10) head.next = Node(20) head.next.next = Node(30) head.next.next.next = Node(40) head = delete_node(head, 2) print_list(head)
Attempts:
2 left
💡 Hint
Remember that position is zero-based and you need to remove the node at that position.
✗ Incorrect
The node at position 2 (0-based) is the node with value 30. After deleting it, the list becomes 10 -> 20 -> 40 -> null.
❓ Predict Output
intermediate2:00remaining
Result after deleting head node in a linked list
What is the printed linked list after deleting the node at position 0 (head node)?
DSA Python
class Node: def __init__(self, data): self.data = data self.next = None def delete_node(head, position): if position == 0: return head.next current = head count = 0 while current is not None and count < position - 1: current = current.next count += 1 if current is None or current.next is None: return head current.next = current.next.next return head def print_list(head): current = head result = [] while current: result.append(str(current.data)) current = current.next print(' -> '.join(result) + ' -> null') # Create linked list 5 -> 15 -> 25 -> null head = Node(5) head.next = Node(15) head.next.next = Node(25) head = delete_node(head, 0) print_list(head)
Attempts:
2 left
💡 Hint
Deleting position 0 means removing the head node.
✗ Incorrect
Deleting the head node (position 0) removes the node with value 5. The new head is the node with value 15, so the list becomes 15 -> 25 -> null.
❓ Predict Output
advanced2:00remaining
Output after deleting node at invalid position
What is the printed linked list after trying to delete node at position 5 in a list of length 3?
DSA Python
class Node: def __init__(self, data): self.data = data self.next = None def delete_node(head, position): if position == 0: return head.next current = head count = 0 while current is not None and count < position - 1: current = current.next count += 1 if current is None or current.next is None: return head current.next = current.next.next return head def print_list(head): current = head result = [] while current: result.append(str(current.data)) current = current.next print(' -> '.join(result) + ' -> null') # Create linked list 1 -> 2 -> 3 -> null head = Node(1) head.next = Node(2) head.next.next = Node(3) head = delete_node(head, 5) print_list(head)
Attempts:
2 left
💡 Hint
Deleting a node at a position outside the list length should not change the list.
✗ Incorrect
Position 5 is beyond the list length (3), so no deletion happens. The list remains 1 -> 2 -> 3 -> null.
🔧 Debug
advanced2:00remaining
Identify the error in delete_node function
What error will this code raise when deleting node at position 3 in a 3-node list?
DSA Python
class Node: def __init__(self, data): self.data = data self.next = None def delete_node(head, position): if position == 0: return head.next current = head count = 0 while current.next is not None and count < position - 1: current = current.next count += 1 current.next = current.next.next return head def print_list(head): current = head result = [] while current: result.append(str(current.data)) current = current.next print(' -> '.join(result) + ' -> null') # Create linked list 7 -> 8 -> 9 -> null head = Node(7) head.next = Node(8) head.next.next = Node(9) head = delete_node(head, 3) print_list(head)
Attempts:
2 left
💡 Hint
Check what happens when current.next is None and you try to access current.next.next.
✗ Incorrect
When position is 3, the loop stops at the last node (position 2). current.next is None, so current.next.next raises AttributeError on NoneType.
🚀 Application
expert3:00remaining
Number of nodes after multiple deletions
Given the linked list 1 -> 2 -> 3 -> 4 -> 5 -> null, after deleting nodes at positions 1, 2, and 0 in that order, how many nodes remain?
DSA Python
class Node: def __init__(self, data): self.data = data self.next = None def delete_node(head, position): if position == 0: return head.next current = head count = 0 while current is not None and count < position - 1: current = current.next count += 1 if current is None or current.next is None: return head current.next = current.next.next return head # Create linked list 1 -> 2 -> 3 -> 4 -> 5 -> null head = Node(1) head.next = Node(2) head.next.next = Node(3) head.next.next.next = Node(4) head.next.next.next.next = Node(5) head = delete_node(head, 1) # delete node at position 1 head = delete_node(head, 2) # delete node at position 2 head = delete_node(head, 0) # delete node at position 0 # Count nodes count = 0 current = head while current: count += 1 current = current.next print(count)
Attempts:
2 left
💡 Hint
Perform each deletion step by step and count nodes after all deletions.
✗ Incorrect
After deleting position 1: list is 1 -> 3 -> 4 -> 5
After deleting position 2: list is 1 -> 3 -> 5
After deleting position 0: list is 3 -> 5
Number of nodes remaining is 2.