Bird
Raised Fist0
DSA C++programming~20 mins

Delete Node at Specific Position in DSA C++ - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Delete Node Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output 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;
}
A20 -> 30 -> 40 -> 50 -> null
B10 -> 20 -> 30 -> 50 -> null
C10 -> 20 -> 30 -> 40 -> null
D10 -> 20 -> 40 -> 50 -> null
Attempts:
2 left
💡 Hint
Remember that position 3 means the third node in the list starting from 1.
🧠 Conceptual
intermediate
1:00remaining
Number 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?
A4
B5
C1
D0
Attempts:
2 left
💡 Hint
Deleting the first node removes exactly one node from the list.
🔧 Debug
advanced
2:00remaining
Identify 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;
}
ALogical error: deletes wrong node
BCompilation error due to missing semicolon
CSegmentation fault (accessing null pointer)
DNo error, node deleted successfully
Attempts:
2 left
💡 Hint
Check if the code verifies that current or current->next is null before accessing.
❓ Predict Output
advanced
2:00remaining
Output 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;
}
A1 -> 2 -> 3 -> 4 -> null
B1 -> 2 -> 3 -> null
C2 -> 3 -> 4 -> 5 -> null
D1 -> 2 -> 3 -> 4 -> 5 -> null
Attempts:
2 left
💡 Hint
Deleting position 5 removes the last node.
🧠 Conceptual
expert
1:30remaining
Time 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?
AO(n-k)
BO(k)
CO(1)
DO(n)
Attempts:
2 left
💡 Hint
You must traverse the list to reach the node before position k.

Practice

(1/5)
1. What happens when you delete a node at position 1 in a singly linked list?
easy
A. The head node is removed and the next node becomes the new head
B. The last node is removed
C. The list remains unchanged
D. All nodes are deleted

Solution

  1. Step 1: Understand position 1 in linked list

    Position 1 refers to the head node of the list.
  2. Step 2: Delete the head node

    Removing the head means the next node becomes the new head.
  3. Final Answer:

    The head node is removed and the next node becomes the new head -> Option A
  4. Quick Check:

    Deleting position 1 removes the head [OK]
Hint: Deleting position 1 means removing the head node [OK]
Common Mistakes:
  • Thinking position 1 deletes the last node
  • Assuming list stays same after deletion
  • Deleting all nodes instead of one
2. Which of the following is the correct syntax to delete a node at position pos in a singly linked list in C++?
easy
A. head = head->prev;
B. if (pos == 1) { head = head->next; }
C. delete head->next->prev;
D. head->next = NULL;

Solution

  1. Step 1: Check deletion at position 1

    If position is 1, we update head to head->next to remove the first node.
  2. Step 2: Verify syntax correctness

    if (pos == 1) { head = head->next; } correctly updates head pointer; others are invalid or incorrect for singly linked list.
  3. Final Answer:

    if (pos == 1) { head = head->next; } -> Option B
  4. Quick Check:

    Update head for position 1 deletion [OK]
Hint: For position 1, assign head = head->next [OK]
Common Mistakes:
  • Using prev pointer in singly linked list
  • Setting next pointer to NULL incorrectly
  • Deleting wrong node pointer
3. What is the output linked list after deleting the node at position 3 from the list: 1 -> 2 -> 3 -> 4 -> 5 -> NULL?
medium
A. 2 -> 3 -> 4 -> 5 -> NULL
B. 1 -> 2 -> 3 -> 5 -> NULL
C. 1 -> 3 -> 4 -> 5 -> NULL
D. 1 -> 2 -> 4 -> 5 -> NULL

Solution

  1. Step 1: Identify node at position 3

    Node at position 3 is the node with value 3.
  2. Step 2: Remove node 3 and link previous to next

    Link node 2's next to node 4, skipping node 3.
  3. Final Answer:

    1 -> 2 -> 4 -> 5 -> NULL -> Option D
  4. Quick Check:

    Node 3 removed, list links 2 to 4 [OK]
Hint: Skip node at position by linking previous to next [OK]
Common Mistakes:
  • Removing wrong node
  • Not updating previous node's next pointer
  • Deleting head instead of position 3
4. Given the code snippet below, what is the error when deleting a node at position 4?
void deleteNode(Node*& head, int pos) {
  Node* temp = head;
  for (int i = 1; i < pos - 1; i++) {
    temp = temp->next;
  }
  Node* toDelete = temp->next;
  temp->next = toDelete->next;
  delete toDelete;
}
medium
A. No check if position is valid or if temp is NULL
B. Incorrect loop range causing off-by-one error
C. Deleting head node incorrectly
D. Using delete on a pointer not allocated with new

Solution

  1. Step 1: Analyze loop and pointer usage

    The loop moves temp to node before position to delete, but no checks if temp or temp->next is NULL.
  2. Step 2: Identify missing validation

    Without checking if position is valid or temp is NULL, code may crash on invalid positions.
  3. Final Answer:

    No check if position is valid or if temp is NULL -> Option A
  4. Quick Check:

    Missing boundary checks cause runtime errors [OK]
Hint: Always check pointers before accessing next [OK]
Common Mistakes:
  • Ignoring invalid position inputs
  • Assuming list length is sufficient
  • Not checking for NULL pointers
5. You have a singly linked list: 10 -> 20 -> 30 -> 40 -> 50 -> NULL. Write the resulting list after deleting the node at position 5 and then deleting the node at position 1.
hard
A. 20 -> 30 -> 40 -> 50 -> NULL
B. 10 -> 20 -> 30 -> 40 -> NULL
C. 20 -> 30 -> 40 -> NULL
D. 10 -> 20 -> 30 -> NULL

Solution

  1. Step 1: Delete node at position 5

    Node at position 5 is 50, removing it results in list: 10 -> 20 -> 30 -> 40 -> NULL.
  2. Step 2: Delete node at position 1

    Deleting head node 10 updates head to 20, final list: 20 -> 30 -> 40 -> NULL.
  3. Final Answer:

    20 -> 30 -> 40 -> NULL -> Option C
  4. Quick Check:

    Delete last then head node sequentially [OK]
Hint: Delete last node first, then head node for correct order [OK]
Common Mistakes:
  • Deleting head first then last causing wrong list
  • Not updating head after deleting position 1
  • Forgetting to remove last node
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard