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 top node from the linked list?
Consider a singly linked list with nodes containing values 10 -> 20 -> 30 -> null. The pop operation removes the first node. What is the state of the list after pop?
DSA Java
class Node { int data; Node next; Node(int data) { this.data = data; this.next = null; } } class LinkedList { Node head; void push(int data) { Node newNode = new Node(data); newNode.next = head; head = newNode; } int pop() { if (head == null) return -1; int val = head.data; head = head.next; return val; } String printList() { StringBuilder sb = new StringBuilder(); Node current = head; while (current != null) { sb.append(current.data).append(" -> "); current = current.next; } sb.append("null"); return sb.toString(); } } public class Main { public static void main(String[] args) { LinkedList list = new LinkedList(); list.push(30); list.push(20); list.push(10); list.pop(); System.out.println(list.printList()); } }
Attempts:
2 left
💡 Hint
Remember, pop removes the first node (head) from the list.
✗ Incorrect
The list is built by pushing 30, then 20, then 10, so the list is 10 -> 20 -> 30 -> null. Popping removes the head node (10), so the new head is 20, resulting in 20 -> 30 -> null.
❓ Predict Output
intermediateWhat is the output after popping twice from the linked list?
Given the same linked list built by pushing 30, 20, 10, what is the list state after two pop operations?
DSA Java
class Node { int data; Node next; Node(int data) { this.data = data; this.next = null; } } class LinkedList { Node head; void push(int data) { Node newNode = new Node(data); newNode.next = head; head = newNode; } int pop() { if (head == null) return -1; int val = head.data; head = head.next; return val; } String printList() { StringBuilder sb = new StringBuilder(); Node current = head; while (current != null) { sb.append(current.data).append(" -> "); current = current.next; } sb.append("null"); return sb.toString(); } } public class Main { public static void main(String[] args) { LinkedList list = new LinkedList(); list.push(30); list.push(20); list.push(10); list.pop(); list.pop(); System.out.println(list.printList()); } }
Attempts:
2 left
💡 Hint
Each pop removes the head node.
✗ Incorrect
First pop removes 10, second pop removes 20, leaving 30 as the head node.
🔧 Debug
advancedWhat error occurs when popping from an empty linked list?
Consider the pop method below. What happens if pop is called when the list is empty (head is null)?
DSA Java
int pop() { int val = head.data; head = head.next; return val; }
Attempts:
2 left
💡 Hint
Accessing a field on a null object causes an error.
✗ Incorrect
If head is null, accessing head.data causes a NullPointerException at runtime.
❓ Predict Output
advancedWhat is the output after popping all nodes from the linked list?
Starting with a linked list built by pushing 1, 2, 3, 4, what is the list state after popping four times?
DSA Java
class Node { int data; Node next; Node(int data) { this.data = data; this.next = null; } } class LinkedList { Node head; void push(int data) { Node newNode = new Node(data); newNode.next = head; head = newNode; } int pop() { if (head == null) return -1; int val = head.data; head = head.next; return val; } String printList() { StringBuilder sb = new StringBuilder(); Node current = head; while (current != null) { sb.append(current.data).append(" -> "); current = current.next; } sb.append("null"); return sb.toString(); } } public class Main { public static void main(String[] args) { LinkedList list = new LinkedList(); list.push(4); list.push(3); list.push(2); list.push(1); list.pop(); list.pop(); list.pop(); list.pop(); System.out.println(list.printList()); } }
Attempts:
2 left
💡 Hint
Popping all nodes empties the list.
✗ Incorrect
After popping all four nodes, head becomes null, so the list is empty and prints null.
🧠 Conceptual
expertWhat is the time complexity of the pop operation on a singly linked list?
Consider a singly linked list where pop removes the head node. What is the time complexity of pop in terms of number of nodes n?
Attempts:
2 left
💡 Hint
Pop removes the first node without traversal.
✗ Incorrect
Pop removes the head node directly without traversing the list, so it runs in constant time O(1).
