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 2 in a singly linked list
What is the printed state of the linked list after deleting the node at position 2?
DSA C#
using System; class Node { public int data; public Node next; public Node(int d) { data = d; next = null; } } class LinkedList { public Node head; public void DeleteNodeAtPosition(int position) { if (head == null) return; Node temp = head; if (position == 0) { head = temp.next; return; } for (int i = 0; temp != null && i < position - 1; i++) { temp = temp.next; } if (temp == null || temp.next == null) return; temp.next = temp.next.next; } public void PrintList() { Node temp = head; while (temp != null) { Console.Write(temp.data + " -> "); temp = temp.next; } Console.WriteLine("null"); } } class Program { static void Main() { 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.DeleteNodeAtPosition(2); list.PrintList(); } }
Attempts:
2 left
💡 Hint
Remember that position 0 is the head node. Deleting at position 2 removes the third node.
✗ Incorrect
The node at position 2 (0-based) is the node with value 30. After deletion, the list links 20 directly to 40.
❓ Predict Output
intermediateOutput after deleting node at position 0 (head) in a singly linked list
What is the printed state of the linked list after deleting the node at position 0?
DSA C#
using System; class Node { public int data; public Node next; public Node(int d) { data = d; next = null; } } class LinkedList { public Node head; public void DeleteNodeAtPosition(int position) { if (head == null) return; Node temp = head; if (position == 0) { head = temp.next; return; } for (int i = 0; temp != null && i < position - 1; i++) { temp = temp.next; } if (temp == null || temp.next == null) return; temp.next = temp.next.next; } public void PrintList() { Node temp = head; while (temp != null) { Console.Write(temp.data + " -> "); temp = temp.next; } Console.WriteLine("null"); } } class Program { static void Main() { LinkedList list = new LinkedList(); list.head = new Node(5); list.head.next = new Node(15); list.head.next.next = new Node(25); list.DeleteNodeAtPosition(0); list.PrintList(); } }
Attempts:
2 left
💡 Hint
Deleting at position 0 removes the head node.
✗ Incorrect
The head node with value 5 is removed, so the new head is the node with value 15.
🔧 Debug
advancedIdentify the error when deleting a node at an invalid position
What error or behavior occurs when trying to delete a node at position 5 in a list of 3 nodes?
DSA C#
using System; class Node { public int data; public Node next; public Node(int d) { data = d; next = null; } } class LinkedList { public Node head; public void DeleteNodeAtPosition(int position) { if (head == null) return; Node temp = head; if (position == 0) { head = temp.next; return; } for (int i = 0; temp != null && i < position - 1; i++) { temp = temp.next; } if (temp == null || temp.next == null) return; temp.next = temp.next.next; } } class Program { static void Main() { LinkedList list = new LinkedList(); list.head = new Node(1); list.head.next = new Node(2); list.head.next.next = new Node(3); list.DeleteNodeAtPosition(5); Console.WriteLine("Deletion attempted at position 5"); } }
Attempts:
2 left
💡 Hint
Check the conditions that stop deletion if position is invalid.
✗ Incorrect
The code checks if temp or temp.next is null before deletion. If position is out of range, it returns without error.
❓ Predict Output
advancedOutput after deleting last node in a singly linked list
What is the printed state of the linked list after deleting the node at the last position?
DSA C#
using System; class Node { public int data; public Node next; public Node(int d) { data = d; next = null; } } class LinkedList { public Node head; public void DeleteNodeAtPosition(int position) { if (head == null) return; Node temp = head; if (position == 0) { head = temp.next; return; } for (int i = 0; temp != null && i < position - 1; i++) { temp = temp.next; } if (temp == null || temp.next == null) return; temp.next = temp.next.next; } public void PrintList() { Node temp = head; while (temp != null) { Console.Write(temp.data + " -> "); temp = temp.next; } Console.WriteLine("null"); } } class Program { static void Main() { LinkedList list = new LinkedList(); list.head = new Node(7); list.head.next = new Node(14); list.head.next.next = new Node(21); list.head.next.next.next = new Node(28); list.DeleteNodeAtPosition(3); list.PrintList(); } }
Attempts:
2 left
💡 Hint
Deleting at position 3 removes the last node in a 4-node list.
✗ Incorrect
The node with value 28 at position 3 is removed, so the list ends at 21.
🧠 Conceptual
expertEffect of deleting a node at a position greater than list length
If you try to delete a node at a position greater than the length of the singly linked list, what is the expected behavior of the given DeleteNodeAtPosition method?
DSA C#
public void DeleteNodeAtPosition(int position) { if (head == null) return; Node temp = head; if (position == 0) { head = temp.next; return; } for (int i = 0; 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
Look at the checks for temp and temp.next before deletion.
✗ Incorrect
If position is out of range, temp or temp.next becomes null and the method returns without deleting any node.
