A doubly linked list is a chain of nodes where each node has two links: one to the next node and one to the previous node. This allows moving forward and backward through the list.
Click to reveal answer
beginner
What happens when you delete the first node in a doubly linked list?
The first node is removed, the head pointer moves to the next node, and the new head's previous link is set to None to disconnect from the old first node.
Click to reveal answer
intermediate
Why do we need to update the previous pointer of the new head when deleting from the beginning?
Because the new head should not point back to the old first node, setting its previous pointer to None ensures the list starts cleanly from the new head.
Click to reveal answer
beginner
What should you check before deleting the first node in a doubly linked list?
You should check if the list is empty (head is None). If empty, there is nothing to delete.
Click to reveal answer
intermediate
Show the Rust code snippet to delete the first node from a doubly linked list safely.
```rust
fn delete_from_beginning(head: &mut Option<Box<Node>>) {
if let Some(mut node) = head.take() {
*head = node.next.take();
if let Some(ref mut new_head) = head {
new_head.prev = None;
}
}
}
```
Click to reveal answer
What pointer must be updated when deleting the first node in a doubly linked list?
AThe previous pointer of the new head
BThe next pointer of the old head
CThe previous pointer of the old tail
DThe next pointer of the new tail
✗ Incorrect
The previous pointer of the new head must be set to None to disconnect from the old first node.
What should you do if the doubly linked list is empty and you try to delete from the beginning?
AThrow an error
BDo nothing, as there is no node to delete
CDelete the tail node instead
DCreate a new node
✗ Incorrect
If the list is empty, there is no node to delete, so the operation should do nothing.
In Rust, what does the method `take()` do when deleting the first node?
AIt swaps the node with the tail
BIt clones the node
CIt replaces the Option with None and returns the original value
DIt deletes the node permanently
✗ Incorrect
`take()` replaces the Option with None and returns the original value, allowing safe ownership transfer.
After deleting the first node, what happens to the head pointer?
AIt points to the second node
BIt points to the old first node
CIt becomes None
DIt points to the tail
✗ Incorrect
The head pointer moves to the second node, which becomes the new first node.
Why is a doubly linked list better than a singly linked list for deletion at the beginning?
ABecause it cannot delete nodes
BBecause it uses less memory
CBecause it has only one pointer per node
DBecause it allows easy backward traversal and pointer updates
✗ Incorrect
Doubly linked lists have pointers to both next and previous nodes, making deletion and traversal easier.
Explain step-by-step how to delete the first node from a doubly linked list.
Think about what pointers need to change to keep the list connected.
You got /4 concepts.
Write a simple Rust function to delete the first node from a doubly linked list and explain each line.
Focus on ownership and pointer updates in Rust.
You got /4 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 moves to the next node in the list
C. It becomes null immediately
D. It remains unchanged
Solution
Step 1: Understand the role of head in a doubly linked list
The head pointer always points to the first node in the list.
Step 2: Effect of deleting the first node
When the first node is deleted, the head must move to the next node to keep the list accessible.
Final Answer:
It moves to the next node in the list -> Option B
Quick Check:
Deleting first node updates head to next node [OK]
Hint: Head always points to first node; update it after deletion [OK]
Common Mistakes:
Not updating head pointer after deletion
Setting head to null without checking list length
Confusing head with tail pointer
2. Which of the following Rust code snippets correctly deletes the first node from a doubly linked list?
easy
A. if let Some(mut node) = head.take() { head = node.next.take(); if let Some(ref mut new_head) = head { new_head.prev = None; } }
B. head = head.next; head.prev = None;
C. head = None; tail = None;
D. head.prev = None; head = head.next;
Solution
Step 1: Analyze safe Rust deletion pattern
if let Some(mut node) = head.take() { head = node.next.take(); if let Some(ref mut new_head) = head { new_head.prev = None; } } uses take() to safely remove the first node and updates head and prev pointers correctly.
Step 2: Check other options for errors
head = head.next; head.prev = None; tries to access head.next without handling Option. head = None; tail = None; deletes entire list incorrectly. head.prev = None; head = head.next; accesses head.prev before updating head, causing error.
Final Answer:
Option A code snippet -> Option A
Quick Check:
Safe pointer update with take() = if let Some(mut node) = head.take() { head = node.next.take(); if let Some(ref mut new_head) = head { new_head.prev = None; } } [OK]
Hint: Use take() to safely remove and update pointers [OK]
Common Mistakes:
Ignoring Option unwrap safety
Not updating prev pointer of new head
Setting head to None without checking
3. Given the following Rust code for a doubly linked list deletion from beginning, what will be the printed list after deletion?
struct Node { val: i32, prev: Option, next: Option }
// List: 10 <-> 20 <-> 30
let mut head = Some(Box::new(Node { val: 10, prev: None, next: Some(Box::new(Node { val: 20, prev: None, next: Some(Box::new(Node { val: 30, prev: None, next: None })) })) }));
// Delete first node
if let Some(mut node) = head.take() {
head = node.next.take();
if let Some(ref mut new_head) = head {
new_head.prev = None;
}
}
// Print list
let mut current = &head;
while let Some(node) = current {
print!("{} -> ", node.val);
current = &node.next;
}
print!("null");
medium
A. null
B. 10 -> 20 -> 30 -> null
C. 20 -> 30 -> null
D. 30 -> null
Solution
Step 1: Initial list structure
The list starts as 10 <-> 20 <-> 30, with head at 10.
Step 2: Deletion of first node
Deleting first node moves head to 20, and sets 20.prev to None.
Step 3: Printing the list
Starting from new head (20), printing nodes gives 20 -> 30 -> null.
Final Answer:
20 -> 30 -> null -> Option C
Quick Check:
Head updated to second node, list prints from 20 [OK]
Hint: After deletion, head points to second node; print from there [OK]
Common Mistakes:
Printing old head after deletion
Not updating prev pointer causing broken links
Confusing next and prev pointers
4. Identify the error in this Rust code snippet that tries to delete the first node from a doubly linked list:
if head.is_some() {
head = head.unwrap().next;
if let Some(ref mut new_head) = head {
new_head.prev = None;
}
}
medium
A. Using unwrap() consumes head, causing a move error
B. Not setting new_head.prev to None
C. Missing check if head is None before deletion
D. Incorrectly updating tail pointer
Solution
Step 1: Understand unwrap() behavior on Option
Calling unwrap() on head moves the value out, consuming it and invalidating head.
Step 2: Consequence of move error
Assigning head = head.unwrap().next; causes a move error because head is used after being moved.
Final Answer:
Using unwrap() consumes head, causing a move error -> Option A
Quick Check:
unwrap() moves value, invalidating head [OK]
Hint: Avoid unwrap() on Option when reassigning; use take() [OK]
Common Mistakes:
Using unwrap() causing move errors
Not handling Option safely
Forgetting to update prev pointer
5. You have a doubly linked list with nodes: 5 <-> 15 <-> 25 <-> 35. After deleting the first node twice using correct Rust code, what will be the final list printed?
let mut head = Some(Box::new(Node { val: 5, prev: None, next: Some(Box::new(Node { val: 15, prev: None, next: Some(Box::new(Node { val: 25, prev: None, next: Some(Box::new(Node { val: 35, prev: None, next: None })) })) })) }));
// Delete first node twice
for _ in 0..2 {
if let Some(mut node) = head.take() {
head = node.next.take();
if let Some(ref mut new_head) = head {
new_head.prev = None;
}
}
}
// Print list
let mut current = &head;
while let Some(node) = current {
print!("{} -> ", node.val);
current = &node.next;
}
print!("null");
hard
A. 15 -> 25 -> 35 -> null
B. null
C. 35 -> null
D. 25 -> 35 -> null
Solution
Step 1: Initial list and first deletion
List starts as 5 <-> 15 <-> 25 <-> 35. First deletion removes 5, head moves to 15.
Step 2: Second deletion
Second deletion removes 15, head moves to 25, and 25.prev is set to None.