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 node at position 3
What is the printed linked list after deleting the node at position 3 (1-based index) from the list?
DSA Java
class Node { int data; Node next; Node(int d) { data = d; next = null; } } class LinkedList { Node head; void deleteAtPosition(int position) { if (head == null) return; if (position == 1) { head = head.next; return; } Node temp = head; for (int i = 1; temp != null && i < position - 1; i++) { temp = temp.next; } if (temp == null || temp.next == null) return; temp.next = temp.next.next; } void printList() { Node n = head; while (n != null) { System.out.print(n.data + " -> "); n = n.next; } System.out.print("null\n"); } 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.head.next.next.next = new Node(40); list.head.next.next.next.next = new Node(50); list.deleteAtPosition(3); list.printList(); } }
Attempts:
2 left
💡 Hint
Remember that deleting at position 3 removes the third node from the start.
✗ Incorrect
The node at position 3 has value 30. After deletion, the list links 20 directly to 40, skipping 30.
❓ Predict Output
intermediateOutput after deleting node at position 1
What is the printed linked list after deleting the node at position 1 (head node) from the list?
DSA Java
class Node { int data; Node next; Node(int d) { data = d; next = null; } } class LinkedList { Node head; void deleteAtPosition(int position) { if (head == null) return; if (position == 1) { head = head.next; return; } Node temp = head; for (int i = 1; temp != null && i < position - 1; i++) { temp = temp.next; } if (temp == null || temp.next == null) return; temp.next = temp.next.next; } void printList() { Node n = head; while (n != null) { System.out.print(n.data + " -> "); n = n.next; } System.out.print("null\n"); } public static void main(String[] args) { LinkedList list = new LinkedList(); list.head = new Node(5); list.head.next = new Node(15); list.head.next.next = new Node(25); list.deleteAtPosition(1); list.printList(); } }
Attempts:
2 left
💡 Hint
Deleting position 1 removes the head node.
✗ Incorrect
The head node with value 5 is removed, so the new head is 15.
🔧 Debug
advancedIdentify the error in deleteAtPosition method
What error will occur when running this code if position is greater than the length of the list?
DSA Java
void deleteAtPosition(int position) { if (head == null) return; if (position == 1) { head = head.next; return; } Node temp = head; for (int i = 1; i < position - 1; i++) { temp = temp.next; } temp.next = temp.next.next; }
Attempts:
2 left
💡 Hint
Think about what happens if temp or temp.next is null.
✗ Incorrect
If position is beyond list length, temp or temp.next becomes null, causing NullPointerException when accessing temp.next.next.
❓ Predict Output
advancedOutput after deleting node at last position
What is the printed linked list after deleting the node at the last position from the list?
DSA Java
class Node { int data; Node next; Node(int d) { data = d; next = null; } } class LinkedList { Node head; void deleteAtPosition(int position) { if (head == null) return; if (position == 1) { head = head.next; return; } Node temp = head; for (int i = 1; temp != null && i < position - 1; i++) { temp = temp.next; } if (temp == null || temp.next == null) return; temp.next = temp.next.next; } void printList() { Node n = head; while (n != null) { System.out.print(n.data + " -> "); n = n.next; } System.out.print("null\n"); } public static void main(String[] args) { LinkedList list = new LinkedList(); list.head = new Node(1); list.head.next = new Node(2); list.head.next.next = new Node(3); list.deleteAtPosition(3); list.printList(); } }
Attempts:
2 left
💡 Hint
Deleting last node removes the tail.
✗ Incorrect
The node with value 3 is removed, so the list ends at 2.
🧠 Conceptual
expertEffect of deleting node at invalid position
If you try to delete a node at position 10 in a linked list of length 5 using the given deleteAtPosition method, what will be the state of the linked list after the operation?
DSA Java
void deleteAtPosition(int position) { if (head == null) return; if (position == 1) { head = head.next; return; } Node temp = head; for (int i = 1; temp != null && i < position - 1; i++) { temp = temp.next; } if (temp == null || temp.next == null) return; temp.next = temp.next.next; }
Attempts:
2 left
💡 Hint
Check the condition that prevents deletion if position is invalid.
✗ Incorrect
The method returns early if position is invalid, so the list stays the same.
