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 moving forward and backward through the list.
Click to reveal answer
beginner
What are the main steps to insert a node at a specific position in a doubly linked list?
1. Traverse the list to reach the node just before the desired position. 2. Create a new node. 3. Adjust the new node's next and previous pointers. 4. Update the surrounding nodes' pointers to include the new node.
Click to reveal answer
intermediate
Why do we need to update both next and previous pointers when inserting in a doubly linked list?
Because each node points to both its next and previous nodes, updating both pointers ensures the list stays connected correctly in both directions after insertion.
Click to reveal answer
intermediate
What happens if you try to insert at a position greater than the list length?
If the position is beyond the list length, the insertion should either be rejected or the new node can be added at the end of the list, depending on the implementation.
Click to reveal answer
beginner
How do you insert a node at the head (position 1) of a doubly linked list?
Create the new node, set its next pointer to the current head, set the current head's previous pointer to the new node (if head is not null), then update the head pointer to the new node.
Click to reveal answer
What pointers must be updated when inserting a node in the middle of a doubly linked list?
AOnly previous node's next pointer
BNew node's next and previous, previous node's next, next node's previous
COnly new node's next pointer
DOnly next node's previous pointer
✗ Incorrect
When inserting, the new node's next and previous pointers must be set, and the surrounding nodes' pointers must be updated to include the new node.
If you insert at position 1 in a doubly linked list, what happens to the head pointer?
AIt moves to the new node
BIt becomes null
CIt moves to the second node
DIt stays the same
✗ Incorrect
Inserting at position 1 means the new node becomes the head, so the head pointer must be updated to point to the new node.
What is the time complexity to insert at a specific position in a doubly linked list?
AO(n^2)
BO(1)
CO(log n)
DO(n)
✗ Incorrect
You may need to traverse up to n nodes to reach the position, so insertion at a specific position is O(n).
Which pointer in a doubly linked list node points to the previous node?
Anext
Bhead
Cprev
Dtail
✗ Incorrect
The 'prev' pointer points to the previous node in a doubly linked list.
What should you do if the position to insert is beyond the current list length?
AInsert at the end or reject the insertion
BInsert at the head
CIgnore the position and do nothing
DDelete the list
✗ Incorrect
Common practice is to insert at the end or reject the insertion if the position is invalid.
Explain step-by-step how to insert a new node at a specific position in a doubly linked list.
Think about how pointers connect nodes before and after insertion.
You got /5 concepts.
Describe how the pointers change when inserting a node at the head of a doubly linked list.
Focus on the first node and how the list starts.
You got /4 concepts.
Practice
(1/5)
1. What happens when you insert a new node at position 1 in a doubly linked list?
easy
A. The list remains unchanged.
B. The new node becomes the new head of the list.
C. The new node replaces the last node.
D. The new node is added at the end of the list.
Solution
Step 1: Understand position 1 in doubly linked list
Position 1 means the start of the list, so the new node should be the first node.
Step 2: Insert new node at head
The new node's next pointer points to the old head, and the old head's previous pointer points back to the new node.
Final Answer:
The new node becomes the new head of the list. -> Option B
Quick Check:
Insert at position 1 = new head [OK]
Hint: Position 1 means insert at the start [OK]
Common Mistakes:
Inserting at position 1 but not updating head pointer
Not updating previous pointer of old head
Confusing position 1 with end of list
2. Which of the following is the correct syntax to insert a new node at position pos in a doubly linked list in C++?
Hint: Update next and prev pointers carefully [OK]
Common Mistakes:
Mixing up next and prev pointers
Not updating temp->next->prev pointer
Assigning pointers in wrong order causing broken links
3. Given the doubly linked list: 10 <-> 20 <-> 30 <-> null, what will be the list after inserting 15 at position 2?
void insertAtPosition(Node*& head, int data, int pos) {
Node* newNode = new Node(data);
if (pos == 1) {
newNode->next = head;
if (head) head->prev = newNode;
head = newNode;
return;
}
Node* temp = head;
for (int i = 1; i < pos - 1 && temp != nullptr; i++) {
temp = temp->next;
}
if (temp == nullptr) return;
newNode->next = temp->next;
if (temp->next) temp->next->prev = newNode;
temp->next = newNode;
newNode->prev = temp;
}
medium
A. 10 <-> 15 <-> 20 <-> 30 <-> null
B. 15 <-> 10 <-> 20 <-> 30 <-> null
C. 10 <-> 20 <-> 15 <-> 30 <-> null
D. 10 <-> 20 <-> 30 <-> 15 <-> null
Solution
Step 1: Locate position 2 in the list
Position 2 means after the first node (10). So temp points to node with data 10.
Step 2: Insert new node with data 15 after temp
New node's next points to 20, and prev points to 10. Update 10's next to new node and 20's prev to new node.
Final Answer:
10 <-> 15 <-> 20 <-> 30 <-> null -> Option A
Quick Check:
Insert at position 2 places new node after first node [OK]
Hint: Position 2 means insert after first node [OK]
Common Mistakes:
Inserting before first node instead of after
Not updating prev pointer of next node
Skipping position count in loop
4. Identify the error in the following code snippet for inserting at position pos in a doubly linked list:
void insertAtPosition(Node*& head, int data, int pos) {
Node* newNode = new Node(data);
if (pos == 1) {
newNode->next = head;
if (head) head->prev = newNode;
head = newNode;
return;
}
Node* temp = head;
for (int i = 1; i < pos - 1; i++) {
temp = temp->next;
}
newNode->next = temp->next;
temp->next = newNode;
newNode->prev = temp;
if (newNode->next) newNode->next->prev = newNode;
}
medium
A. The newNode->next pointer is assigned after temp->next is changed.
B. The newNode->prev pointer is not set correctly.
C. The head pointer is not updated when inserting at position 1.
D. The loop does not check if temp is nullptr, causing possible crash.
Solution
Step 1: Analyze the for loop traversal
The loop moves temp forward but does not check if temp becomes nullptr, risking null pointer dereference.
Step 2: Identify missing safety check
Without checking temp != nullptr, accessing temp->next can cause a crash if pos is invalid or list is short.
Final Answer:
The loop does not check if temp is nullptr, causing possible crash. -> Option D
Quick Check:
Always check temp != nullptr in traversal loops [OK]
Hint: Always check for nullptr when traversing [OK]
Common Mistakes:
Ignoring nullptr checks in loops
Not updating head for position 1 insert
Incorrect order of pointer assignments
5. You have a doubly linked list: 5 <-> 10 <-> 15 <-> 20 <-> null. You want to insert a new node with value 12 at position 3. After insertion, what will be the correct sequence of the list? Also, explain how to handle insertion at the last position if the position is greater than the list length.
hard
A. 5 <-> 12 <-> 10 <-> 15 <-> 20 <-> null; Insert at last by replacing last node.
B. 5 <-> 10 <-> 15 <-> 12 <-> 20 <-> null; Insert at last by adding node before head.
C. 5 <-> 10 <-> 12 <-> 15 <-> 20 <-> null; Insert at last by traversing till last node and updating pointers.
D. 5 <-> 10 <-> 15 <-> 20 <-> 12 <-> null; Insert at last by ignoring position.
Solution
Step 1: Insert 12 at position 3
Position 3 means after second node (10). Insert new node between 10 and 15, updating next and prev pointers accordingly.
Step 2: Handle insertion at last position if position > length
Traverse to last node, then insert new node after it, updating last node's next and new node's prev pointers.
Final Answer:
5 <-> 10 <-> 12 <-> 15 <-> 20 <-> null; Insert at last by traversing till last node and updating pointers. -> Option C
Quick Check:
Insert at position 3 places node between 10 and 15; last insert by traversal [OK]
Hint: Insert at position or append if position too large [OK]
Common Mistakes:
Inserting at wrong position
Not updating prev pointer of next node
Ignoring last position insertion logic
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.