📖 Scenario: You are working with a simple linked list that stores numbers. You want to find the middle number in the list, just like finding the middle person in a line.
🎯 Goal: Build a Rust program that creates a linked list, sets up a way to find the middle element, and then prints that middle element.
📋 What You'll Learn
Create a linked list with exactly 5 nodes containing the values 10, 20, 30, 40, 50 in that order.
Create a variable to help find the middle element using the two-pointer technique.
Write the logic to find the middle element by moving pointers through the list.
Print the value of the middle node.
💡 Why This Matters
🌍 Real World
Finding the middle element of a linked list is useful in many applications like splitting data into halves, balancing tasks, or optimizing searches.
💼 Career
Understanding linked lists and pointer techniques is important for software developers working with low-level data structures, memory management, and algorithm optimization.
Progress0 / 4 steps
1
Create the Linked List
Create a linked list with nodes containing the values 10, 20, 30, 40, and 50 in that order. Use the provided Node struct and create a variable called head pointing to the first node.
DSA Rust
Hint
Start from the last node with value: 50 and next: None, then wrap each node inside the previous one using Some(Box::new(Node { ... })).
2
Set Up Pointers to Find Middle
Create two variables called slow and fast and set both to point to head.as_ref(). These will help you find the middle element by moving through the list at different speeds.
DSA Rust
Hint
Use head.as_ref() to get a reference to the first node, then assign it to both slow and fast variables.
3
Find the Middle Element Using Two Pointers
Write a while loop that moves fast two steps and slow one step forward until fast reaches the end of the list. Use fast.unwrap().next.as_ref() to move fast and slow.unwrap().next.as_ref() to move slow.
DSA Rust
Hint
Check that fast and fast.unwrap().next are Some before moving pointers. Move slow one step and fast two steps inside the loop.
4
Print the Middle Element
Print the value of the middle node using slow.unwrap().value. Use println!("{}", slow.unwrap().value).
DSA Rust
Hint
Use println!("{}", slow.unwrap().value) to print the middle node's value.
Practice
(1/5)
1. What is the main idea behind finding the middle element of a singly linked list using two pointers?
easy
A. Count all nodes first, then access the middle by index
B. Use one slow pointer moving one step and one fast pointer moving two steps until fast reaches the end
C. Reverse the list and pick the first element
D. Use a stack to store elements and pop half of them
Solution
Step 1: Understand two-pointer technique
The slow pointer moves one node at a time, while the fast pointer moves two nodes at a time.
Step 2: Identify middle when fast pointer reaches the end
When the fast pointer reaches the end of the list, the slow pointer will be at the middle node.
Final Answer:
Use one slow pointer moving one step and one fast pointer moving two steps until fast reaches the end -> Option B
Quick Check:
Two-pointer technique = Use one slow pointer moving one step and one fast pointer moving two steps until fast reaches the end [OK]
Hint: Move fast pointer twice as fast as slow pointer [OK]
Common Mistakes:
Trying to count nodes first wastes time
Reversing list is unnecessary
Using extra space like stack is inefficient
2. Which of the following Rust code snippets correctly initializes a singly linked list node with value 5?
easy
A. let node = Node::new(5, None);
B. let node = Node(value: 5, next: None);
C. let node = Node { value: 5, next: None };
D. let node = Node { 5, None };
Solution
Step 1: Recall Rust struct initialization syntax
Rust structs are initialized with field names and values inside curly braces.