📖 Scenario: Imagine you have a line of people waiting to buy tickets. Sometimes, the first person in line leaves the line. We want to model this line using a linked list and remove the first person when they leave.
🎯 Goal: You will create a linked list with three nodes, then remove the first node (person at the beginning of the line), and finally print the remaining list.
📋 What You'll Learn
Create a linked list with nodes containing values 10, 20, and 30 in that order
Create a variable called head that points to the first node
Write code to remove the first node by updating head
Print the linked list after deletion in the format: 20 -> 30 -> null
💡 Why This Matters
🌍 Real World
Linked lists are used in many programs to manage sequences of data where items are added or removed frequently, like managing tasks or undo history.
💼 Career
Understanding linked lists and how to manipulate nodes is fundamental for software developers working with data structures, memory management, and system programming.
Progress0 / 4 steps
1
Create the linked list with three nodes
Create a struct called Node with a field value of type i32 and a field next of type Option>. Then create three nodes with values 10, 20, and 30 linked together. Finally, create a variable called head that points to the first node.
DSA Rust
Hint
Use Option> for the next field to allow linking nodes. Create nodes from last to first so you can link them easily.
2
Create a mutable variable head to allow deletion
Change the variable head to be mutable by adding mut so you can update it later to delete the first node.
DSA Rust
Hint
Add mut before head to allow changing it later.
3
Delete the first node by updating head
Update head to point to the second node by setting head to head.and_then(|node| node.next). This removes the first node from the list.
DSA Rust
Hint
Use head = head.and_then(|node| node.next) to move head to the next node.
4
Print the linked list after deleting the first node
Write a loop to print the values of the linked list starting from head. Print each value followed by -> and end with null. Use a variable current to traverse the list.
DSA Rust
Hint
Use a while let loop to traverse and print each node's value followed by ->. End with null.
Practice
(1/5)
1. What does deleting a node at the beginning of a singly linked list do to the list?
easy
A. Deletes all nodes in the list
B. Removes the last node and updates the tail
C. Removes the first node and updates the head to the next node
D. Removes a node from the middle of the list
Solution
Step 1: Understand the head pointer role
The head points to the first node in the list, so deleting the first node means changing the head.
Step 2: Update the head to the next node
After removing the first node, the head should point to the second node, effectively removing the first node from the list.
Final Answer:
Removes the first node and updates the head to the next node -> Option C
Quick Check:
Delete at beginning = remove first node [OK]
Hint: Deleting at start means moving head to next node [OK]
Common Mistakes:
Trying to remove the last node instead of the first
Not updating the head pointer after deletion
Deleting all nodes instead of just one
2. Which Rust code snippet correctly deletes the first node of a singly linked list stored in a variable head?
easy
A. head = head.take().and_then(|node| node.next);
B. head = head.unwrap().next;
C. head = head.next;
D. head.next = head;
Solution
Step 1: Understand Rust Option handling for linked lists
In Rust, linked lists often use Option. To delete the first node, we take the head and replace it with its next node safely.
Step 2: Use take() and and_then() to update head
head.take() removes the current head, and and_then(|node| node.next) sets head to the next node, deleting the first.
Final Answer:
head = head.take().and_then(|node| node.next); -> Option A
Quick Check:
Use take() and and_then() to delete first node [OK]
Hint: Use take() and and_then() to update head safely [OK]
Common Mistakes:
Trying to assign head.next directly without handling Option
Using unwrap() which can panic if head is None
Assigning head.next to head causing infinite loop
3. What will be the output linked list after deleting the first node from this Rust linked list?
let mut head = Some(Box::new(Node { value: 10, next: Some(Box::new(Node { value: 20, next: Some(Box::new(Node { value: 30, next: None })) })) }));
head = head.take().and_then(|node| node.next);
Print the list values in order.
medium
A. []
B. [10, 20, 30]
C. [30]
D. [20, 30]
Solution
Step 1: Initial list values
The list starts as 10 -> 20 -> 30 -> None.
Step 2: Delete the first node
Using head.take().and_then(|node| node.next) removes the node with value 10 and sets head to node with value 20.
Final Answer:
[20, 30] -> Option D
Quick Check:
After deletion, list starts at 20 [OK]
Hint: Deleting first node removes head value [OK]
Common Mistakes:
Forgetting to update head, so list stays same
Assuming deletion removes last node
Confusing node values order after deletion
4. Identify the error in this Rust code that tries to delete the first node of a linked list:
fn delete_first(mut head: Option) -> Option {
head = head.unwrap().next;
head
}
medium
A. Using unwrap() without checking if head is None causes panic
B. The function does not return the updated head
C. head.next is assigned incorrectly
D. The function should take &mut head instead of ownership
Solution
Step 1: Check unwrap() usage
Calling unwrap() on an Option that might be None will cause the program to panic at runtime.
Step 2: Safe deletion requires handling None
Before unwrap(), code should check if head is Some or use methods like take() and and_then() to avoid panic.
Final Answer:
Using unwrap() without checking if head is None causes panic -> Option A
Quick Check:
unwrap() on None causes panic [OK]
Hint: Never unwrap() without checking Option first [OK]
Common Mistakes:
Ignoring possibility of empty list (None)
Not returning updated head properly
Confusing ownership and borrowing in function
5. Given a Rust singly linked list where each node contains an integer, write the correct way to delete the first node and return the updated list head. Which code snippet correctly handles the edge case when the list is empty?
hard
A. fn delete_first(head: Option) -> Option {
head.unwrap().next
}
B. fn delete_first(mut head: Option) -> Option {
head = head.take().and_then(|node| node.next);
head
}
C. fn delete_first(head: Option) -> Option {
if head.is_some() {
head.unwrap()
} else {
None
}
}
D. fn delete_first(head: Option) -> Option {
head.next
}
Solution
Step 1: Handle Option safely with take() and and_then()
Using head.take().and_then(|node| node.next) safely removes the first node and returns the next node or None if empty.
Step 2: Return updated head
Assign the result back to head and return it, ensuring the function works for empty and non-empty lists.
Final Answer:
fn delete_first(mut head: Option) -> Option {
head = head.take().and_then(|node| node.next);
head
} -> Option B
Quick Check:
Use take() and and_then() for safe deletion [OK]
Hint: Use take() and and_then() to handle empty list safely [OK]
Common Mistakes:
Using unwrap() without checking for None
Accessing head.next directly on Option type
Not returning the updated head after deletion
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.