Challenge - 5 Problems
Linked List Pop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after popping the head node?
Consider a singly linked list with nodes 10 -> 20 -> 30 -> null. After popping the head node, what will be the new list?
DSA C#
class Node { public int data; public Node next; public Node(int data) { this.data = data; this.next = null; } } class LinkedList { public Node head; public void Push(int data) { Node newNode = new Node(data); newNode.next = head; head = newNode; } public int Pop() { if (head == null) throw new InvalidOperationException("List is empty"); int val = head.data; head = head.next; return val; } public override string ToString() { Node current = head; string result = ""; while (current != null) { result += current.data + " -> "; current = current.next; } return result + "null"; } } LinkedList list = new LinkedList(); list.Push(30); list.Push(20); list.Push(10); list.Pop(); Console.WriteLine(list.ToString());
Attempts:
2 left
💡 Hint
Popping removes the first node (head) from the list.
✗ Incorrect
Initially, nodes are pushed in order 30, 20, 10, so list is 10 -> 20 -> 30 -> null. Popping removes 10, so new head is 20.
❓ Predict Output
intermediateWhat is the output after two pops on the linked list?
Given a linked list with nodes 5 -> 15 -> 25 -> null, what is the list after popping twice?
DSA C#
class Node { public int data; public Node next; public Node(int data) { this.data = data; this.next = null; } } class LinkedList { public Node head; public void Push(int data) { Node newNode = new Node(data); newNode.next = head; head = newNode; } public int Pop() { if (head == null) throw new InvalidOperationException("List is empty"); int val = head.data; head = head.next; return val; } public override string ToString() { Node current = head; string result = ""; while (current != null) { result += current.data + " -> "; current = current.next; } return result + "null"; } } LinkedList list = new LinkedList(); list.Push(25); list.Push(15); list.Push(5); list.Pop(); list.Pop(); Console.WriteLine(list.ToString());
Attempts:
2 left
💡 Hint
Each pop removes the head node.
✗ Incorrect
Pushing 25, 15, 5 results in list 5 -> 15 -> 25 -> null. Two pops remove 5 and then 15, leaving 25 -> null.
🔧 Debug
advancedWhat error occurs when popping from an empty linked list?
Consider the pop method below. What error will be raised if pop is called on an empty list?
DSA C#
public int Pop() { if (head == null) throw new InvalidOperationException("List is empty"); int val = head.data; head = head.next; return val; } LinkedList list = new LinkedList(); list.Pop();
Attempts:
2 left
💡 Hint
Check the condition when head is null.
✗ Incorrect
The code explicitly throws InvalidOperationException when head is null, so that error occurs.
❓ Predict Output
advancedWhat is the output after popping all nodes one by one?
Given a linked list with nodes 1 -> 2 -> 3 -> null, what is printed after popping all nodes and printing each popped value?
DSA C#
class Node { public int data; public Node next; public Node(int data) { this.data = data; this.next = null; } } class LinkedList { public Node head; public void Push(int data) { Node newNode = new Node(data); newNode.next = head; head = newNode; } public int Pop() { if (head == null) throw new InvalidOperationException("List is empty"); int val = head.data; head = head.next; return val; } } LinkedList list = new LinkedList(); list.Push(3); list.Push(2); list.Push(1); while (true) { try { Console.Write(list.Pop() + " "); } catch (InvalidOperationException) { break; } }
Attempts:
2 left
💡 Hint
Pop removes from the head, which is the last pushed node.
✗ Incorrect
Nodes pushed: 3, then 2, then 1. List is 1 -> 2 -> 3. Popping removes 1, then 2, then 3, printing in that order.
🧠 Conceptual
expertWhat is the time complexity of popping the head node in a singly linked list?
Consider a singly linked list with n nodes. What is the time complexity of the pop operation that removes the head node?
Attempts:
2 left
💡 Hint
Popping the head node only changes a pointer.
✗ Incorrect
Removing the head node requires updating the head pointer only, which is a constant time operation.
