A singly linked list is a chain of nodes where each node holds data and a link to the next node. It starts at a head node and ends when a node points to nothing (null).
Click to reveal answer
beginner
In a singly linked list, what does each node contain?
Each node contains two parts: the data (value) and a pointer (link) to the next node in the list.
Click to reveal answer
beginner
How do you add a new node at the beginning of a singly linked list?
Create a new node, set its next pointer to the current head, then update the head to this new node.
Click to reveal answer
intermediate
Why is a singly linked list different from an array?
A singly linked list stores elements in nodes connected by pointers, allowing easy insertion and deletion without shifting elements, unlike arrays which store elements in contiguous memory.
Click to reveal answer
beginner
What is the role of the 'head' in a singly linked list?
The head is the first node in the list. It acts as the starting point to access all other nodes by following the links.
Click to reveal answer
What does the 'next' pointer in a singly linked list node point to?
AThe next node in the list
BThe previous node in the list
CThe last node in the list
DThe head node
✗ Incorrect
In a singly linked list, each node's 'next' pointer points to the next node in the sequence.
How do you know when you have reached the end of a singly linked list?
AWhen the current node's next pointer is null
BWhen the current node's data is zero
CWhen the current node points back to the head
DWhen the list has only one node
✗ Incorrect
The end of a singly linked list is marked by a node whose next pointer is null (no next node).
Which operation is easiest to perform on a singly linked list?
AInsert at the end without tail pointer
BDelete the last node without previous pointer
CAccess element by index
DInsert at the beginning
✗ Incorrect
Inserting at the beginning is simple because you only update the new node's next to the current head and then update head.
What is the initial value of the head pointer when a singly linked list is empty?
Aundefined
B0
Cnull
Dhead points to itself
✗ Incorrect
When the list is empty, the head pointer is null, meaning no nodes exist.
In Rust, what keyword is used to create a new node struct for a singly linked list?
Afn
Bstruct
Cenum
Dclass
✗ Incorrect
Rust uses the 'struct' keyword to define custom data types like nodes.
Explain how to create a singly linked list from scratch in Rust, including the node structure and how to add a node at the front.
Think about how each node points to the next and how the head starts the list.
You got /5 concepts.
Describe the difference between a singly linked list and an array in terms of memory and operations.
Consider how elements are stored and accessed.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a Singly Linked List in data structures?
easy
A. To store elements in a random order without any links
B. To store elements in a tree-like hierarchical structure
C. To store elements with direct access by index like an array
D. To store elements in a linear order where each element points to the next
Solution
Step 1: Understand the structure of a singly linked list
A singly linked list stores elements in nodes where each node points to the next node, forming a chain.
Step 2: Compare with other data structures
Unlike arrays, singly linked lists do not provide direct index access and unlike trees, they have a single linear chain of nodes.
Final Answer:
To store elements in a linear order where each element points to the next -> Option D
Quick Check:
Singly Linked List = Linear chain with next pointers [OK]
Hint: Remember: singly linked list nodes point only forward [OK]
Common Mistakes:
Confusing linked lists with arrays having direct index access
Thinking linked lists are hierarchical like trees
Assuming nodes can point backward in singly linked lists
2. Which of the following is the correct way to define a node struct for a singly linked list in Rust?
easy
A. struct Node { value: i32, next: &Node }
B. struct Node { value: i32, next: Node }
C. struct Node { value: i32, next: Option }
D. struct Node { value: i32, next: Vec<Node> }
Solution
Step 1: Understand Rust ownership and pointers
Rust requires heap allocation for recursive types like linked lists, so Box is used to own the next node.
Step 2: Analyze each option
struct Node { value: i32, next: Option } uses Option allowing for a nullable next pointer, which is correct. struct Node { value: i32, next: Node } is invalid because it causes infinite size. struct Node { value: i32, next: &Node } uses a reference without lifetime, which is invalid. struct Node { value: i32, next: Vec<Node> } uses a vector which is not suitable for singly linked list next pointer.
Hint: Use Option for nullable next node in Rust [OK]
Common Mistakes:
Using direct Node type causes infinite size error
Using references without lifetimes for next pointer
Using Vec<Node> instead of a single next node pointer
3. What will be the printed output after running this Rust code that creates a singly linked list with values 1, 2, 3 and prints them?
struct Node {
value: i32,
next: Option,
}
fn main() {
let third = Node { value: 3, next: None };
let second = Node { value: 2, next: Some(Box::new(third)) };
let first = Node { value: 1, next: Some(Box::new(second)) };
let mut current = &first;
while let Some(ref next_node) = current.next {
println!("{} ->", current.value);
current = next_node;
}
println!("{} -> null", current.value);
}
medium
A. 1 -> 2 -> null
B. 1 -> 2 -> 3 -> null
C. 3 -> 2 -> 1 -> null
D. Compilation error due to borrowing
Solution
Step 1: Trace the linked list creation
Nodes are created in reverse order: third (3, None), second (2, Some(third)), first (1, Some(second)). So the list is 1 -> 2 -> 3 -> null.
Step 2: Follow the print loop
The loop prints current.value followed by '->' while current.next is Some. It prints 1 ->, then 2 ->, then exits loop at 3 (next is None) and prints 3 -> null.
Final Answer:
1 -> 2 -> 3 -> null -> Option B
Quick Check:
Prints nodes in order from first to last [OK]
Hint: Trace node links from first to last for output [OK]
Common Mistakes:
Printing nodes in reverse order by mistake
Missing the last node print with 'null'
Confusing Option with direct node causing errors
4. Identify the error in this Rust code snippet that tries to create a singly linked list node:
A. next field must be Option, but node1 is Node type
B. Missing semicolon after node2 declaration
C. Cannot move node1 because it is not boxed inside Option
D. No error, code compiles fine
Solution
Step 1: Check the type of next field
The next field expects an Option, but node1 is a Node, not wrapped in Box or Option.
Step 2: Understand Rust type requirements
Rust requires the next pointer to be boxed and wrapped in Option to handle ownership and nullability. Directly assigning node1 causes a type mismatch.
Final Answer:
next field must be Option, but node1 is Node type -> Option A
Quick Check:
next must be Option not Node [OK]
Hint: Match next field type exactly: Option [OK]
Common Mistakes:
Assigning Node directly instead of Option
Ignoring Rust ownership rules for recursive types
Assuming semicolon missing causes error here
5. You want to implement a method append to add a new node at the end of a singly linked list in Rust. Which approach correctly handles ownership and mutability to traverse and append the node?
hard
A. Use a mutable reference to Option and loop until next is None, then set next to Some(new_node)
B. Use an immutable reference to Node and assign next directly without mutability
C. Clone the entire list and add the new node at the front
D. Use unsafe pointers to bypass Rust ownership rules
Solution
Step 1: Understand Rust ownership and mutability for linked lists
To modify the list, you need a mutable reference to the next pointer (Option) to traverse and update it safely.
Step 2: Analyze each option
Use a mutable reference to Option and loop until next is None, then set next to Some(new_node) correctly uses mutable references to traverse until the end and then sets next to Some(new_node). Use an immutable reference to Node and assign next directly without mutability uses immutable reference which cannot modify. Clone the entire list and add the new node at the front clones the list which is inefficient and not appending at end. Use unsafe pointers to bypass Rust ownership rules uses unsafe code which is discouraged.
Final Answer:
Use a mutable reference to Option and loop until next is None, then set next to Some(new_node) -> Option A
Quick Check:
Mutable reference to next pointer needed for append [OK]
Hint: Traverse with mutable Option reference to append [OK]
Common Mistakes:
Using immutable references preventing modification
Trying to assign next without Option or Box
Using unsafe code unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.