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 steps are needed to insert a new node at the beginning of a doubly linked list?
1. Create a new node with the given data. 2. Set the new node's next pointer to the current head. 3. Set the new node's previous pointer to null. 4. If the list is not empty, set the current head's previous pointer to the new node. 5. Update the head pointer to the new node.
Click to reveal answer
intermediate
Why do we need to update the previous pointer of the old head node when inserting at the beginning?
Because the new node becomes the first node, the old head now comes after it. Its previous pointer must point back to the new node to maintain the doubly linked structure.
Click to reveal answer
beginner
What happens if the doubly linked list is empty when inserting at the beginning?
The new node becomes the only node in the list. Its next and previous pointers are set to null, and the head points to this new node.
Click to reveal answer
beginner
Show the C++ code snippet to insert a node at the beginning of a doubly linked list.
```cpp
struct Node {
int data;
Node* next;
Node* prev;
};
void insertAtBeginning(Node*& head, int newData) {
Node* newNode = new Node();
newNode->data = newData;
newNode->prev = nullptr;
newNode->next = head;
if (head != nullptr) {
head->prev = newNode;
}
head = newNode;
}
```
Click to reveal answer
What pointer of the new node should be set to nullptr when inserting at the beginning?
Anone
Bnext
Cboth prev and next
Dprev
✗ Incorrect
The new node is the first node, so its previous pointer should be nullptr. Its next pointer points to the old head.
When inserting at the beginning, what must be updated in the old head node?
AIts prev pointer to new node
BIts data value
CIts next pointer to new node
DNothing
✗ Incorrect
The old head's previous pointer must point to the new node to maintain the doubly linked list structure.
If the list is empty, what will be the next pointer of the new node after insertion at beginning?
Anullptr
BPoints to itself
CPoints to old head
DUndefined
✗ Incorrect
Since the list is empty, the new node's next pointer is nullptr.
Which of these is NOT a step in inserting at the beginning of a doubly linked list?
ACreate new node
BSet new node's next to head
CSet head's next to new node
DUpdate head to new node
✗ Incorrect
We do not set head's next to new node; instead, we set new node's next to head.
What is the time complexity of inserting a node at the beginning of a doubly linked list?
AO(n)
BO(1)
CO(log n)
DO(n^2)
✗ Incorrect
Insertion at the beginning is done in constant time O(1) because it involves only pointer updates.
Explain the process of inserting a new node at the beginning of a doubly linked list.
Think about how pointers change when adding a new first node.
You got /5 concepts.
Why is it important to update the previous pointer of the old head node when inserting at the beginning?
Consider how doubly linked lists allow moving both ways.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of inserting a node at the beginning of a doubly linked list?
easy
A. To delete the first node of the list
B. To add a new node before the current head node
C. To add a new node after the tail node
D. To reverse the entire list
Solution
Step 1: Understand the head node position
The head node is the first node in the doubly linked list.
Step 2: Insert new node before head
Inserting at the beginning means placing the new node before the current head and updating pointers.
Final Answer:
To add a new node before the current head node -> Option B
Quick Check:
Insert at beginning = Add before head [OK]
Hint: Insert at beginning means new node becomes the head [OK]
Common Mistakes:
Confusing insertion at beginning with insertion at end
Not updating the previous head's previous pointer
Trying to delete instead of insert
2. Which of the following is the correct syntax to create a new node and insert it at the beginning of a doubly linked list in C++?
easy
A. Node* newNode = new Node(data); head->prev = newNode; newNode->next = head; head = newNode;
B. Node newNode = Node(data); newNode->next = head; head = newNode;
C. Node* newNode = new Node(data); head->next = newNode; newNode->prev = head; head = newNode;
D. Node* newNode = new Node(data); newNode->next = head; head = newNode;
Solution
Step 1: Create new node with pointer
Use pointer syntax: Node* newNode = new Node(data);
Step 2: Update pointers for doubly linked list
Set newNode->next to current head, update head->prev to newNode, then update head to newNode.
Final Answer:
Node* newNode = new Node(data); head->prev = newNode; newNode->next = head; head = newNode; -> Option A
Quick Check:
Correct pointer updates = Node* newNode = new Node(data); head->prev = newNode; newNode->next = head; head = newNode; [OK]
Hint: Remember to update both next and prev pointers [OK]
Common Mistakes:
Using object instead of pointer for new node
Not updating head->prev pointer
Swapping next and prev pointers incorrectly
3. What will be the output of the following code snippet after inserting 10 at the beginning of the doubly linked list?
struct Node {
int data;
Node* prev;
Node* next;
Node(int d) : data(d), prev(nullptr), next(nullptr) {}
};
Node* head = new Node(20);
head->next = new Node(30);
head->next->prev = head;
Node* newNode = new Node(10);
newNode->next = head;
head->prev = newNode;
head = newNode;
// Print list from head
Node* temp = head;
while(temp) {
std::cout << temp->data << " -> ";
temp = temp->next;
}
std::cout << "null";
medium
A. 30 -> 20 -> 10 -> null
B. 20 -> 30 -> 10 -> null
C. 10 -> 20 -> 30 -> null
D. 10 -> 30 -> 20 -> null
Solution
Step 1: Initial list setup
List initially: 20 <-> 30, head points to 20.
Step 2: Insert 10 at beginning
New node 10 points next to 20, 20's prev points to 10, head updated to 10.
Step 3: Print list from head
Traverse from head: 10 -> 20 -> 30 -> null
Final Answer:
10 -> 20 -> 30 -> null -> Option C
Quick Check:
New head is 10, followed by 20 and 30 [OK]
Hint: New node becomes head, old head shifts right [OK]
Common Mistakes:
Not updating head pointer after insertion
Incorrect pointer assignments causing wrong order
Printing from tail instead of head
4. Identify the error in the following code snippet that inserts a node at the beginning of a doubly linked list:
void insertAtBeginning(Node*& head, int data) {
Node* newNode = new Node(data);
newNode->next = head;
head->prev = newNode;
head = newNode;
}
medium
A. newNode->prev should be set to head
B. newNode->next should be set to nullptr
C. head pointer should not be passed by reference
D. Missing check if head is nullptr before accessing head->prev
Solution
Step 1: Analyze pointer usage
Code accesses head->prev without checking if head is nullptr (empty list).
Step 2: Identify potential runtime error
If head is nullptr, accessing head->prev causes a crash (null pointer dereference).
Final Answer:
Missing check if head is nullptr before accessing head->prev -> Option D
Quick Check:
Always check head != nullptr before head->prev [OK]
Hint: Check if list is empty before updating pointers [OK]
Common Mistakes:
Ignoring nullptr checks causing crashes
Incorrect pointer assignments for prev
Passing head by value instead of reference
5. Given a doubly linked list with nodes containing values [5, 15, 25], write the sequence of node values after inserting 1 at the beginning and then 35 at the beginning. What is the final list?
hard
A. 35 -> 1 -> 5 -> 15 -> 25 -> null
B. 1 -> 35 -> 5 -> 15 -> 25 -> null
C. 5 -> 15 -> 25 -> 1 -> 35 -> null
D. 35 -> 5 -> 15 -> 25 -> 1 -> null
Solution
Step 1: Initial list
List is 5 -> 15 -> 25 -> null.
Step 2: Insert 1 at beginning
New head is 1, list becomes 1 -> 5 -> 15 -> 25 -> null.
Step 3: Insert 35 at beginning
New head is 35, list becomes 35 -> 1 -> 5 -> 15 -> 25 -> null.
Final Answer:
35 -> 1 -> 5 -> 15 -> 25 -> null -> Option A
Quick Check:
Last inserted node is new head [OK]
Hint: Last inserted node at beginning becomes new head [OK]
Common Mistakes:
Inserting nodes at end instead of beginning
Not updating head pointer after each insertion
Mixing order of inserted nodes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.