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 happens when you delete the last node from a doubly linked list?
The last node is removed, the second last node's next pointer is set to null, and if the list becomes empty, the head pointer is also set to null.
Click to reveal answer
intermediate
Why do we need to update the previous node's next pointer when deleting from the end of a doubly linked list?
Because each node points back to the previous node, when the last node is removed, the new last node's next pointer must be set to null, and its previous pointer remains unchanged to maintain list integrity.
Click to reveal answer
intermediate
Show the TypeScript code snippet to delete the last node from a doubly linked list.
```typescript
function deleteFromEnd(head: Node | null): Node | null {
if (!head) return null;
if (!head.next) return null; // only one node
let current = head;
while (current.next) {
current = current.next;
}
if (current.prev) {
current.prev.next = null;
}
return head;
}
```
Click to reveal answer
intermediate
What is the time complexity of deleting the last node in a doubly linked list if we do not keep a tail pointer?
O(n), because we need to traverse from the head to the last node to delete it.
Click to reveal answer
What pointer must be updated when deleting the last node in a doubly linked list?
AThe head pointer only
BThe next node's previous pointer
CThe previous node's next pointer
DNo pointers need updating
✗ Incorrect
The previous node's next pointer must be set to null to remove the last node.
If the doubly linked list has only one node and we delete from the end, what is the new head?
AThe same node
BThe previous node
CThe next node
DNull
✗ Incorrect
Deleting the only node makes the list empty, so the head becomes null.
What is the time complexity of deleting the last node if the list has a tail pointer?
AO(n)
BO(1)
CO(log n)
DO(n^2)
✗ Incorrect
With a tail pointer, deletion from the end is direct and takes constant time.
Which of these is NOT a part of a doubly linked list node?
APointer to head node
BPointer to next node
CPointer to previous node
DData
✗ Incorrect
Nodes do not have a pointer to the head node.
What happens if you forget to update the previous node's next pointer when deleting the last node?
AThe last node remains linked, causing errors
BThe list becomes circular
CThe list becomes empty
DNothing happens
✗ Incorrect
The last node would still be linked, causing incorrect list structure.
Explain step-by-step how to delete the last node from a doubly linked list without a tail pointer.
Think about how to find the last node and what pointers to change.
You got /5 concepts.
Describe the difference in deleting the last node when a tail pointer is present versus when it is not.
Focus on how the tail pointer changes the process.
You got /4 concepts.
Practice
(1/5)
1. What happens to the tail pointer when you delete the last node from a doubly linked list?
easy
A. It becomes null immediately
B. It moves to the previous node of the deleted node
C. It points to the head node
D. It remains unchanged
Solution
Step 1: Understand the tail pointer role
The tail pointer always points to the last node in the list.
Step 2: Effect of deleting the last node
When the last node is deleted, the tail must move to the previous node to remain the last node.
Final Answer:
It moves to the previous node of the deleted node -> Option B
Quick Check:
Tail updates to previous node after deletion [OK]
Hint: Tail always points to last node, so move it back after deletion [OK]
Common Mistakes:
Setting tail to null without checking if list is empty
Not updating tail pointer after deletion
Confusing head and tail pointers
Assuming tail points to head after deletion
2. Which of the following is the correct TypeScript syntax to delete the last node from a doubly linked list assuming tail is not null?
easy
A. tail = tail.prev; tail.next = null;
B. tail = tail.next; tail.prev = null;
C. tail = null; head = null;
D. tail.prev = tail; tail.next = null;
Solution
Step 1: Move tail pointer to previous node
To delete the last node, tail must point to its previous node: tail = tail.prev;
Step 2: Set new tail's next to null
Since tail is now last node, its next must be null: tail.next = null;
Final Answer:
tail = tail.prev; tail.next = null; -> Option A
Quick Check:
Correct syntax moves tail back and nullifies next [OK]
Hint: Tail moves back, then tail.next set to null [OK]
Common Mistakes:
Moving tail forward instead of backward
Not setting tail.next to null
Setting tail to null without updating links
Confusing prev and next pointers
3. Given the doubly linked list: 1 <-> 2 <-> 3 <-> 4 <-> null, what is the list after deleting the last node?
medium
A. 1 <-> 2 <-> 3 <-> null
B. 1 <-> 2 <-> 3 <-> 4 <-> null
C. 2 <-> 3 <-> 4 <-> null
D. 1 <-> 2 <-> null
Solution
Step 1: Identify the last node
The last node is 4, pointed by tail.
Step 2: Delete last node and update tail
Remove node 4, update tail to node 3, set tail.next to null.
Final Answer:
1 <-> 2 <-> 3 <-> null -> Option A
Quick Check:
Last node 4 removed, tail now 3 [OK]
Hint: Remove last node, tail moves to previous node [OK]
Common Mistakes:
Not updating tail pointer
Leaving deleted node linked
Removing wrong node
Confusing head and tail updates
4. Consider this TypeScript code snippet to delete the last node from a doubly linked list: