Bird
Raised Fist0
DSA C++programming~20 mins

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

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Delete Node at Beginning Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output 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 C++
struct Node {
    int data;
    Node* next;
};

void deleteAtBeginning(Node*& head) {
    if (head == nullptr) return;
    Node* temp = head;
    head = head->next;
    delete temp;
}

void printList(Node* head) {
    while (head != nullptr) {
        std::cout << head->data << " -> ";
        head = head->next;
    }
    std::cout << "null" << std::endl;
}

int main() {
    Node* head = new Node{1, new Node{2, new Node{3, nullptr}}};
    deleteAtBeginning(head);
    printList(head);
    return 0;
}
Anull
B1 -> 2 -> 3 -> null
C3 -> null
D2 -> 3 -> null
Attempts:
2 left
💡 Hint
Deleting the first node moves the head pointer to the next node.
❓ Predict Output
intermediate
2:00remaining
Output after deleting the first node in a doubly linked list
What is the printed state of the doubly linked list after deleting the first node?
DSA C++
struct Node {
    int data;
    Node* next;
    Node* prev;
};

void deleteAtBeginning(Node*& head) {
    if (head == nullptr) return;
    Node* temp = head;
    head = head->next;
    if (head != nullptr) head->prev = nullptr;
    delete temp;
}

void printList(Node* head) {
    while (head != nullptr) {
        std::cout << head->data << " <-> ";
        head = head->next;
    }
    std::cout << "null" << std::endl;
}

int main() {
    Node* third = new Node{3, nullptr, nullptr};
    Node* second = new Node{2, third, nullptr};
    third->prev = second;
    Node* head = new Node{1, second, nullptr};
    second->prev = head;
    deleteAtBeginning(head);
    printList(head);
    return 0;
}
Anull
B2 <-> 3 <-> null
C3 <-> null
D1 <-> 2 <-> 3 <-> null
Attempts:
2 left
💡 Hint
After deleting the first node, update the previous pointer of the new head to nullptr.
🔧 Debug
advanced
2:00remaining
Identify the error in deleting the first node of a singly linked list
What error will this code produce when deleting the first node?
DSA C++
struct Node {
    int data;
    Node* next;
};

void deleteAtBeginning(Node* head) {
    if (head == nullptr) return;
    Node* temp = head;
    head = head->next;
    delete temp;
}

int main() {
    Node* head = new Node{1, new Node{2, nullptr}};
    deleteAtBeginning(head);
    std::cout << head->data << std::endl;
    return 0;
}
ARuntime error (use of deleted pointer)
BPrints 1
CCompilation error
DPrints 2
Attempts:
2 left
💡 Hint
Check if the head pointer is updated outside the function.
❓ Predict Output
advanced
2:00remaining
Output after deleting the first node in a circular linked list
What is the printed state of the circular linked list after deleting the first node?
DSA C++
struct Node {
    int data;
    Node* next;
};

void deleteAtBeginning(Node*& head) {
    if (head == nullptr) return;
    if (head->next == head) {
        delete head;
        head = nullptr;
        return;
    }
    Node* temp = head;
    Node* tail = head;
    while (tail->next != head) {
        tail = tail->next;
    }
    head = head->next;
    tail->next = head;
    delete temp;
}

void printList(Node* head) {
    if (head == nullptr) {
        std::cout << "null" << std::endl;
        return;
    }
    Node* temp = head;
    do {
        std::cout << temp->data << " -> ";
        temp = temp->next;
    } while (temp != head);
    std::cout << "(back to head)" << std::endl;
}

int main() {
    Node* third = new Node{3, nullptr};
    Node* second = new Node{2, third};
    Node* head = new Node{1, second};
    third->next = head;
    deleteAtBeginning(head);
    printList(head);
    return 0;
}
A2 -> 3 -> (back to head)
B1 -> 2 -> 3 -> (back to head)
C3 -> (back to head)
Dnull
Attempts:
2 left
💡 Hint
After deleting the first node, update the tail's next pointer to the new head.
🧠 Conceptual
expert
1:00remaining
Effect of deleting the first node on linked list length
If a singly linked list has N nodes, what is the length of the list after deleting the first node?
AN + 1
BN
CN - 1
D0
Attempts:
1 left
💡 Hint
Deleting one node reduces the count by one.

Practice

(1/5)
1. What happens when you delete the first node of a singly linked list?
easy
A. The head pointer moves to the second node, and the first node is removed.
B. The last node is deleted instead of the first.
C. The list becomes empty regardless of its size.
D. The second node is deleted, but the head remains the same.

Solution

  1. Step 1: Understand the head pointer role

    The head pointer points to the first node in the list.
  2. Step 2: Delete the first node by moving head

    To delete the first node, update head to point to the second node, removing the first node from the list.
  3. Final Answer:

    The head pointer moves to the second node, and the first node is removed. -> Option A
  4. Quick Check:

    Deleting first node means head moves forward [OK]
Hint: Deleting first node means head points to next node [OK]
Common Mistakes:
  • Deleting the wrong node (not the first)
  • Not updating the head pointer
  • Assuming list becomes empty always
  • Confusing first and second nodes
2. Which of the following is the correct syntax to delete the first node in a singly linked list in C++?
easy
A. head->next = head; delete head;
B. head = head->next; delete head;
C. delete head; head = head->next;
D. Node* temp = head; head = head->next; delete temp;

Solution

  1. Step 1: Store the current head node

    We save the current head in a temporary pointer to delete it safely later.
  2. Step 2: Move head to next node and delete old head

    Update head to point to the next node, then delete the old head using the temporary pointer.
  3. Final Answer:

    Node* temp = head; head = head->next; delete temp; -> Option D
  4. Quick Check:

    Save old head, move head, delete old head [OK]
Hint: Save old head before deleting to avoid losing access [OK]
Common Mistakes:
  • Deleting head before moving pointer
  • Not saving old head before deleting
  • Assigning head incorrectly
  • Deleting head twice
3. What will be the output linked list after executing the following code?
struct Node {
  int data;
  Node* next;
};

Node* head = new Node{10, new Node{20, new Node{30, nullptr}}};

Node* temp = head;
head = head->next;
delete temp;

Node* curr = head;
while(curr != nullptr) {
  std::cout << curr->data << " -> ";
  curr = curr->next;
}
std::cout << "null";
medium
A. 20 -> 30 -> null
B. null
C. 30 -> null
D. 10 -> 20 -> 30 -> null

Solution

  1. Step 1: Initial linked list setup

    The list starts as 10 -> 20 -> 30 -> null.
  2. Step 2: Delete first node and update head

    We save head (10) in temp, move head to 20, then delete node 10.
  3. Step 3: Print remaining list

    Starting from new head (20), we print 20 -> 30 -> null.
  4. Final Answer:

    20 -> 30 -> null -> Option A
  5. Quick Check:

    First node deleted, list starts at 20 [OK]
Hint: After deleting head, list starts from next node [OK]
Common Mistakes:
  • Printing deleted node data
  • Not updating head pointer
  • Deleting wrong node
  • Forgetting to print null at end
4. Identify the error in the following code snippet that deletes the first node of a linked list:
void deleteFirstNode(Node* head) {
  Node* temp = head;
  head = head->next;
  delete temp;
}
medium
A. head->next is not a valid pointer.
B. Deleting temp before moving head causes runtime error.
C. The function does not update the original head pointer outside the function.
D. No error; code works correctly.

Solution

  1. Step 1: Understand pointer passing in C++

    Function receives head by value, so changes inside do not affect original head.
  2. Step 2: Identify missing update to original head

    Updating local head pointer does not update the caller's head, so list head remains unchanged.
  3. Final Answer:

    The function does not update the original head pointer outside the function. -> Option C
  4. Quick Check:

    Pointer passed by value, original head unchanged [OK]
Hint: Pass head by reference to update original pointer [OK]
Common Mistakes:
  • Assuming head changes outside function
  • Deleting node before moving head
  • Confusing pointer and value passing
  • Ignoring nullptr checks
5. Given a singly linked list, write the correct approach to delete the first node safely and update the head pointer in C++. Which of the following code snippets correctly achieves this?
hard
A. void deleteFirstNode(Node* head) { Node* temp = head; head = head->next; delete temp; }
B. void deleteFirstNode(Node*& head) { if (head == nullptr) return; Node* temp = head; head = head->next; delete temp; }
C. void deleteFirstNode(Node** head) { Node* temp = *head; *head = (*head)->next; delete temp; }
D. void deleteFirstNode(Node head) { head = head.next; delete head; }

Solution

  1. Step 1: Pass head by reference to modify original pointer

    Using Node*& head allows the function to update the caller's head pointer directly.
  2. Step 2: Check for empty list and delete first node safely

    Check if head is nullptr to avoid errors, then move head to next node and delete old first node.
  3. Final Answer:

    void deleteFirstNode(Node*& head) { if (head == nullptr) return; Node* temp = head; head = head->next; delete temp; } -> Option B
  4. Quick Check:

    Reference parameter updates head safely [OK]
Hint: Use reference parameter to update head pointer [OK]
Common Mistakes:
  • Passing head by value instead of reference
  • Not checking for empty list
  • Deleting node without updating head
  • Using incorrect pointer syntax
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