Challenge - 5 Problems
Delete Node Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after deleting node at position 3 in a singly linked list
Given the following C++ code that deletes a node at a specific position in a singly linked list, what is the printed output after deleting the node at position 3?
DSA C++
#include <iostream> using namespace std; struct Node { int data; Node* next; Node(int val) : data(val), next(nullptr) {} }; void deleteNodeAtPosition(Node*& head, int pos) { if (!head) return; if (pos == 1) { Node* temp = head; head = head->next; delete temp; return; } Node* current = head; for (int i = 1; current != nullptr && i < pos - 1; i++) { current = current->next; } if (!current || !current->next) return; Node* temp = current->next; current->next = temp->next; delete temp; } void printList(Node* head) { while (head) { cout << head->data << " -> "; head = head->next; } cout << "null" << endl; } int main() { Node* head = new Node(10); head->next = new Node(20); head->next->next = new Node(30); head->next->next->next = new Node(40); head->next->next->next->next = new Node(50); deleteNodeAtPosition(head, 3); printList(head); return 0; }
Attempts:
2 left
💡 Hint
Remember that position 3 means the third node in the list starting from 1.
✗ Incorrect
The node at position 3 has the value 30. After deleting it, the list links 20 directly to 40, so the list becomes 10 -> 20 -> 40 -> 50 -> null.
🧠 Conceptual
intermediateNumber of nodes after deleting at position 1
If a singly linked list has 5 nodes and you delete the node at position 1, how many nodes remain in the list?
Attempts:
2 left
💡 Hint
Deleting the first node removes exactly one node from the list.
✗ Incorrect
Deleting the node at position 1 removes the head node, so the list size decreases by one from 5 to 4.
🔧 Debug
advancedIdentify the error in this delete function
What error will occur when running this code to delete a node at position 4 in a list of 3 nodes?
DSA C++
void deleteNodeAtPosition(Node*& head, int pos) { if (!head) return; if (pos == 1) { Node* temp = head; head = head->next; delete temp; return; } Node* current = head; for (int i = 1; i < pos - 1; i++) { current = current->next; } Node* temp = current->next; current->next = temp->next; delete temp; }
Attempts:
2 left
💡 Hint
Check if the code verifies that current or current->next is null before accessing.
✗ Incorrect
The code does not check if current or current->next is null before accessing them, so accessing current->next when current is null causes a segmentation fault.
❓ Predict Output
advancedOutput after deleting node at position 5 in a 5-node list
What is the output of the following code after deleting the node at position 5?
DSA C++
#include <iostream> using namespace std; struct Node { int data; Node* next; Node(int val) : data(val), next(nullptr) {} }; void deleteNodeAtPosition(Node*& head, int pos) { if (!head) return; if (pos == 1) { Node* temp = head; head = head->next; delete temp; return; } Node* current = head; for (int i = 1; current != nullptr && i < pos - 1; i++) { current = current->next; } if (!current || !current->next) return; Node* temp = current->next; current->next = temp->next; delete temp; } void printList(Node* head) { while (head) { cout << head->data << " -> "; head = head->next; } cout << "null" << endl; } int main() { Node* head = new Node(1); head->next = new Node(2); head->next->next = new Node(3); head->next->next->next = new Node(4); head->next->next->next->next = new Node(5); deleteNodeAtPosition(head, 5); printList(head); return 0; }
Attempts:
2 left
💡 Hint
Deleting position 5 removes the last node.
✗ Incorrect
The node at position 5 (value 5) is deleted, so the list ends at 4.
🧠 Conceptual
expertTime complexity of deleting node at position k in singly linked list
What is the time complexity of deleting a node at position k in a singly linked list of size n?
Attempts:
2 left
💡 Hint
You must traverse the list to reach the node before position k.
✗ Incorrect
To delete the node at position k, you must reach the (k-1)th node, which takes O(k) time in a singly linked list.
