A doubly linked list is a chain of nodes where each node has three parts: data, a pointer to the next node, and a pointer to the previous node. This allows traversal in both directions.
Click to reveal answer
beginner
What happens when you delete the first node in a doubly linked list?
The head pointer moves to the second node, and the new head's previous pointer is set to null. The old first node is removed from memory.
Click to reveal answer
beginner
Why do we set the previous pointer of the new head to null after deletion?
Because the new head is now the first node, it should not point back to any node before it. Setting previous to null marks it as the start.
Click to reveal answer
intermediate
What should you check before deleting the first node in a doubly linked list?
Check if the list is empty (head is null). If empty, no deletion is possible. Also, if there is only one node, deleting it will make the list empty.
Click to reveal answer
intermediate
Show the C++ code snippet to delete the first node of a doubly linked list safely.
if (head == nullptr) return; // empty list
Node* temp = head;
head = head->next;
if (head != nullptr) head->prev = nullptr;
delete temp;
Click to reveal answer
What pointer must be updated after deleting the first node in a doubly linked list?
AThe next pointer of the old head
BThe previous pointer of the old tail
CThe next pointer of the new tail
DThe previous pointer of the new head
✗ Incorrect
After deletion, the new head's previous pointer must be set to null to mark it as the first node.
What should you do if the doubly linked list is empty when deleting from the beginning?
AReturn immediately, no deletion
BThrow an error
CSet head to a new node
DDelete the head anyway
✗ Incorrect
If the list is empty (head is null), there is nothing to delete, so return immediately.
If the list has only one node, what happens after deleting the first node?
AHead points to the same node
BHead points to a new node
CHead becomes null (empty list)
DList becomes circular
✗ Incorrect
Deleting the only node makes the list empty, so head becomes null.
Which of these is NOT a part of a doubly linked list node?
APointer to next node
BPointer to random node
CPointer to previous node
DData
✗ Incorrect
A standard doubly linked list node has data, next pointer, and previous pointer, but no random pointer.
What is the time complexity of deleting the first node in a doubly linked list?
AO(1)
BO(n)
CO(log n)
DO(n^2)
✗ Incorrect
Deleting the first node is a constant time operation because it involves only a few pointer updates.
Explain step-by-step how to delete the first node from a doubly linked list.
Think about what pointers need to change and what happens to the old first node.
You got /5 concepts.
Describe edge cases to consider when deleting from the beginning of a doubly linked list.
What happens if there is no node or just one node?
You got /3 concepts.
Practice
(1/5)
1. What happens to the head pointer when you delete the first node from a doubly linked list?
easy
A. It points to the last node
B. It becomes null immediately
C. It moves to the second node in the list
D. It remains pointing to the deleted node
Solution
Step 1: Understand the head pointer role
The head points to the first node in the doubly linked list.
Step 2: After deletion, update the head pointer
When the first node is deleted, the head must point to the next node, which becomes the new first node.
Final Answer:
It moves to the second node in the list -> Option C
Quick Check:
Head points to new first node after deletion [OK]
Hint: Head always moves to next node after deleting first [OK]
Common Mistakes:
Not updating head pointer after deletion
Setting head to null without checking list size
Leaving head pointing to deleted node
2. Which of the following is the correct syntax to delete the first node in a doubly linked list in C++?
easy
A. head = head->next; delete head->prev; head->prev = nullptr;
B. Node* temp = head; head = head->next; head->prev = nullptr; delete temp;
C. head->prev = nullptr; delete head; head = head->next;
D. delete head; head = head->next; head->prev = nullptr;
Solution
Step 1: Store current head in temp
We save the current first node in a temporary pointer to delete it safely after updating head.
Step 2: Update head and fix previous pointer
Set head to the next node, then set head->prev to nullptr to detach the old first node.
Step 3: Delete the old first node
Finally, delete the node stored in temp to free memory.
Final Answer:
Node* temp = head; head = head->next; head->prev = nullptr; delete temp; -> Option B
Hint: Save head first, update pointers, then delete saved node [OK]
Common Mistakes:
Deleting head before updating pointers
Not setting head->prev to nullptr
Accessing head->prev when head is nullptr
3. What will be the output of the following code snippet after deleting the first node?
struct Node {
int data;
Node* prev;
Node* next;
};
Node* head = new Node{1, nullptr, nullptr};
Node* second = new Node{2, head, nullptr};
head->next = second;
// Delete from beginning
Node* temp = head;
head = head->next;
head->prev = nullptr;
delete temp;
// Print list
Node* curr = head;
while(curr != nullptr) {
std::cout << curr->data << " ";
curr = curr->next;
}
medium
A. 1 2
B. No output (runtime error)
C. 1
D. 2
Solution
Step 1: Initial list setup
List has two nodes: 1 (head) -> 2. Head points to node with data 1.
Step 2: Delete first node and update pointers
Head moves to second node (data 2), and its prev is set to nullptr. The first node is deleted.
Step 3: Print remaining list
Only node with data 2 remains, so output is "2".
Final Answer:
2 -> Option D
Quick Check:
After deletion, list starts at 2 [OK]
Hint: After deleting head, print starts from new head [OK]
Common Mistakes:
Printing deleted node's data
Not updating head->prev to nullptr
Assuming both nodes print after deletion
4. Identify the error in the following code that deletes the first node of a doubly linked list:
void deleteBeginning(Node*& head) {
if (head == nullptr) return;
delete head;
head = head->next;
if (head != nullptr) head->prev = nullptr;
}
medium
A. Deleting head before updating head pointer causes undefined behavior
B. Missing check if head->next is nullptr
C. head->prev should be set to head->next
D. No error, code works fine
Solution
Step 1: Analyze deletion order
The code deletes head first, then tries to access head->next, which is invalid after deletion.
Step 2: Correct order to avoid undefined behavior
First save head->next in a temp pointer, update head, then delete old node.
Final Answer:
Deleting head before updating head pointer causes undefined behavior -> Option A
Quick Check:
Accessing deleted memory causes error [OK]
Hint: Always update pointers before deleting nodes [OK]
Common Mistakes:
Deleting node before updating pointers
Accessing deleted node's members
Not checking for empty list
5. You have a doubly linked list with nodes: 10 <-> 20 <-> 30 <-> 40. After deleting the first node twice from the beginning, what will be the printed list?
hard
A. 30 <-> 40
B. 20 <-> 30 <-> 40
C. 10 <-> 20 <-> 30 <-> 40
D. 40
Solution
Step 1: Initial list nodes
List is 10 <-> 20 <-> 30 <-> 40, head points to 10.
Step 2: First deletion
Delete node 10, head moves to 20, set head->prev = nullptr. List now: 20 <-> 30 <-> 40.
Step 3: Second deletion
Delete node 20, head moves to 30, set head->prev = nullptr. List now: 30 <-> 40.
Final Answer:
30 <-> 40 -> Option A
Quick Check:
Two deletions remove first two nodes [OK]
Hint: Each deletion moves head to next node [OK]
Common Mistakes:
Deleting only once instead of twice
Not updating head->prev after second deletion
Printing old list without deletions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.