What does forward traversal mean in a data structure like a linked list?
Forward traversal means moving from the first element to the last element, visiting each node one by one in order.
Click to reveal answer
beginner
What is backward traversal in the context of a doubly linked list?
Backward traversal means moving from the last element to the first element, visiting each node in reverse order using the previous links.
Click to reveal answer
intermediate
In Rust, which pointer is used to move forward and which is used to move backward in a doubly linked list?
The next pointer is used to move forward, and the prev pointer is used to move backward.
Click to reveal answer
beginner
Why can't you do backward traversal in a singly linked list?
Because singly linked lists only have a next pointer and no prev pointer, so you cannot move backward.
Click to reveal answer
beginner
What is the time complexity of traversing a linked list forward or backward?
The time complexity is O(n) because you visit each node once in order.
Click to reveal answer
Which pointer do you use to move forward in a doubly linked list?
Ahead
Bprev
Cnext
Dtail
✗ Incorrect
The next pointer points to the next node, so it is used to move forward.
Can you traverse backward in a singly linked list?
ANo, because it has no prev pointer
BYes, always
COnly if it is circular
DOnly if it has a tail pointer
✗ Incorrect
Singly linked lists only have a next pointer, so backward traversal is not possible.
What is the starting point for forward traversal in a linked list?
ATail node
BMiddle node
CAny node
DHead node
✗ Incorrect
Forward traversal starts at the head node and moves forward using the next pointers.
What is the time complexity of traversing a linked list of n nodes?
AO(1)
BO(n)
CO(log n)
DO(n^2)
✗ Incorrect
You visit each node once, so the time complexity is O(n).
In Rust, which keyword helps safely handle pointers during traversal?
AOption
Bmut
Clet
Dunsafe
✗ Incorrect
Rust uses Option to safely handle pointers that may or may not exist during traversal.
Explain how forward and backward traversal works in a doubly linked list.
Think about how the next and prev pointers connect nodes.
You got /4 concepts.
Why is backward traversal not possible in a singly linked list? How does this affect its usage?
Consider the pointers available in singly linked lists.
You got /4 concepts.
Practice
(1/5)
1. What does "Traversal Forward and Backward" mean in the context of a doubly linked list?
easy
A. Skipping nodes while moving forward
B. Moving through the list from the first node to the last and then back from the last to the first
C. Only moving from the first node to the last node
D. Moving randomly between nodes
Solution
Step 1: Understand doubly linked list structure
A doubly linked list has nodes connected both forward and backward, allowing movement in both directions.
Step 2: Define traversal forward and backward
Traversal forward means moving from the first node to the last node, and traversal backward means moving from the last node back to the first node.
Final Answer:
Moving through the list from the first node to the last and then back from the last to the first -> Option B
Quick Check:
Forward and backward traversal = Moving both ways [OK]
Hint: Think of walking to the end and then returning back [OK]
Common Mistakes:
Confusing traversal with skipping nodes
Thinking traversal is only forward
Assuming random movement is traversal
2. Which of the following Rust code snippets correctly traverses a doubly linked list forward using a mutable reference?
easy
A. for node in list { println!("{}", node.value); }
B. while let Some(node) = current { println!("{}", node.value); current = node.prev.as_deref_mut(); }
C. while let Some(node) = current { println!("{}", node.value); current = node.next.as_deref_mut(); }
D. loop { println!("{}", current.value); current = current.prev; }
Solution
Step 1: Identify forward traversal in Rust
Forward traversal moves from current node to next node using the next pointer.
Step 2: Check code correctness
while let Some(node) = current { println!("{}", node.value); current = node.next.as_deref_mut(); } uses current = node.next.as_deref_mut() which correctly moves forward. while let Some(node) = current { println!("{}", node.value); current = node.prev.as_deref_mut(); } moves backward, C is invalid syntax for linked list, D loops backward without condition.
Final Answer:
while let Some(node) = current { println!("{}", node.value); current = node.next.as_deref_mut(); } -> Option C
Quick Check:
Forward traversal uses next pointer [OK]
Hint: Forward traversal uses next pointer, backward uses prev [OK]
Common Mistakes:
Using prev pointer for forward traversal
Incorrect loop syntax for linked list
Ignoring option handling in Rust
3. Given the following Rust code for a doubly linked list traversal backward, what will be printed?
struct Node { value: i32, prev: Option, next: Option }
fn traverse_backward(mut current: Option<&Node>) {
while let Some(node) = current {
print!("{} ", node.value);
current = node.prev.as_deref();
}
}
// Assume list nodes with values 1 -> 2 -> 3, current starts at node with value 3
medium
A. 3 2 1
B. 1 2 3
C. 3 1 2
D. 1 3 2
Solution
Step 1: Understand backward traversal
Starting at node with value 3, moving backward means following prev pointers to 2, then 1.
Step 2: Trace the print output
Prints 3 first, then 2, then 1, each followed by a space.
Final Answer:
3 2 1 -> Option A
Quick Check:
Backward traversal prints nodes in reverse order [OK]
Hint: Backward traversal prints nodes from tail to head [OK]
Common Mistakes:
Confusing forward and backward order
Assuming traversal starts at head
Ignoring Option handling in Rust
4. Identify the error in the following Rust code snippet that attempts to traverse a doubly linked list forward:
fn traverse(mut current: Option<&Node>) {
while current.is_some() {
println!("{}", current.unwrap().value);
current = current.unwrap().next.as_deref();
}
}
medium
A. The loop condition should be while current.is_none()
B. println! macro syntax is incorrect
C. next pointer should be accessed with prev instead
D. Using unwrap() multiple times causes panic if current is None
Solution
Step 1: Analyze unwrap() usage
Calling unwrap() twice without checking can panic if current becomes None between calls.
Step 2: Suggest safer approach
Use while let Some(node) = current to safely unwrap once per iteration.
Final Answer:
Using unwrap() multiple times causes panic if current is None -> Option D
Quick Check:
Multiple unwrap() calls risk panic [OK]
Hint: Use while let Some(node) = current to avoid unwrap panic [OK]
Common Mistakes:
Confusing is_some() and is_none()
Accessing wrong pointer for forward traversal
Misusing println! macro
5. You have a doubly linked list with nodes containing values [10, 20, 30, 40]. Write a Rust function that traverses forward to collect values into a vector, then traverses backward from the last node to collect values into another vector. What will be the content of both vectors after traversal?