Challenge - 5 Problems
Linked List Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after deleting the first node in a singly linked list
What is the printed state of the linked list after deleting the first node?
DSA Java
class Node { int data; Node next; Node(int d) { data = d; next = null; } } class LinkedList { Node head; void deleteAtBeginning() { if (head != null) { head = head.next; } } void printList() { Node temp = head; while (temp != null) { System.out.print(temp.data + " -> "); temp = temp.next; } System.out.println("null"); } public static void main(String[] args) { LinkedList list = new LinkedList(); list.head = new Node(10); list.head.next = new Node(20); list.head.next.next = new Node(30); list.deleteAtBeginning(); list.printList(); } }
Attempts:
2 left
💡 Hint
Deleting the first node moves the head pointer to the next node.
✗ Incorrect
Initially, the list is 10 -> 20 -> 30 -> null. Deleting the first node removes 10, so the new head is 20, resulting in 20 -> 30 -> null.
❓ Predict Output
intermediateOutput when deleting from a single-node linked list
What is the printed state of the linked list after deleting the first node when the list has only one node?
DSA Java
class Node { int data; Node next; Node(int d) { data = d; next = null; } } class LinkedList { Node head; void deleteAtBeginning() { if (head != null) { head = head.next; } } void printList() { Node temp = head; while (temp != null) { System.out.print(temp.data + " -> "); temp = temp.next; } System.out.println("null"); } public static void main(String[] args) { LinkedList list = new LinkedList(); list.head = new Node(100); list.deleteAtBeginning(); list.printList(); } }
Attempts:
2 left
💡 Hint
Deleting the only node should leave the list empty.
✗ Incorrect
The list starts with one node: 100 -> null. After deleting the first node, head becomes null, so the list is empty and prints null.
🔧 Debug
advancedIdentify the error in deleting the first node of a linked list
What error will this code produce when deleting the first node of a linked list?
DSA Java
class Node { int data; Node next; Node(int d) { data = d; next = null; } } class LinkedList { Node head; void deleteAtBeginning() { head = head.next.next; } void printList() { Node temp = head; while (temp != null) { System.out.print(temp.data + " -> "); temp = temp.next; } System.out.println("null"); } public static void main(String[] args) { LinkedList list = new LinkedList(); list.head = new Node(1); list.deleteAtBeginning(); list.printList(); } }
Attempts:
2 left
💡 Hint
Check what happens if head.next is null before accessing head.next.next.
✗ Incorrect
The code tries to assign head = head.next.next without checking if head.next is null. If head.next is null, accessing next on it causes NullPointerException.
🧠 Conceptual
advancedEffect of deleting the first node on list length
If a singly linked list has 5 nodes, what will be the length of the list after deleting the first node?
Attempts:
2 left
💡 Hint
Deleting one node reduces the count by one.
✗ Incorrect
Deleting the first node removes one node from the list, so the length decreases from 5 to 4.
🚀 Application
expertResulting list after multiple deletions at beginning
Given a linked list with nodes 5 -> 10 -> 15 -> 20 -> null, what is the printed list after deleting the first node twice?
DSA Java
class Node { int data; Node next; Node(int d) { data = d; next = null; } } class LinkedList { Node head; void deleteAtBeginning() { if (head != null) { head = head.next; } } void printList() { Node temp = head; while (temp != null) { System.out.print(temp.data + " -> "); temp = temp.next; } System.out.println("null"); } public static void main(String[] args) { LinkedList list = new LinkedList(); list.head = new Node(5); list.head.next = new Node(10); list.head.next.next = new Node(15); list.head.next.next.next = new Node(20); list.deleteAtBeginning(); list.deleteAtBeginning(); list.printList(); } }
Attempts:
2 left
💡 Hint
Each deletion removes the current head node.
✗ Incorrect
First deletion removes 5, list becomes 10 -> 15 -> 20 -> null. Second deletion removes 10, list becomes 15 -> 20 -> null.
