Linked lists
A linked list is nodes joined by pointers. Rust's std::collections::LinkedList exists, and for most programs a Vec or VecDeque is the better choice.
Detailed Explanation
A linked list stores each element in its own allocation, with a pointer to the next (and, in a doubly linked list, the previous). Split and splice can be O(1) if you already hold the node pointers. Indexing is O(n). Walking the list is a pointer chase, so caches hate it.
Rust's standard LinkedList<T> is a doubly linked list. push_front, push_back, pop_front, pop_back are O(1). iter is O(n). There is no cheap list[i]. split_off splits at an index in O(n) to find the node, then O(1) to relink. The type exists for the splice-shaped problems and for teaching. It is not the default list.
Ownership makes hand-rolled lists more awkward than in C. A singly linked list of owned nodes is struct Node { value: T, next: Option<Box<Node>> }. That form is fine and is the usual interview structure. A doubly linked list in safe Rust cannot have two Box pointers to the same node. You use Rc<RefCell<Node>> plus Weak for the back edge, or you write unsafe with raw pointers, which is what LinkedList does internally. That cost is a hint: if you need a doubly linked list, prefer the standard one or a Vec of indices.
When would you still want one? An LRU cache with a map from keys to list nodes (usually a crate, not a from-scratch list). A running splice of huge lists where you cannot afford to move elements. A textbook exercise. For a queue, VecDeque. For a stack, Vec. For "I might insert in the middle", still Vec unless measurements say the moves dominate, which they rarely do for small T.
When you reach for LinkedList: proven splice-heavy workload, or an algorithm that is defined in terms of nodes. When you reach for Vec/VecDeque: the default. When you reach for Rc<RefCell<Node>>: a graph-like structure you accept will be ugly.
Code Examples
struct Node {
value: i32,
next: Option<Box<Node>>,
}
fn sum(mut node: Option<&Node>) -> i32 {
let mut total = 0;
while let Some(n) = node {
total += n.value;
node = n.next.as_deref();
}
total
}
fn main() {
let list = Node {
value: 1,
next: Some(Box::new(Node { value: 2, next: None })),
};
println!("{}", sum(Some(&list))); // prints: 3
}Common Gotchas
`LinkedList` is rarely the right default
A `VecDeque` queue and a `Vec` stack beat it on almost every real workload because of cache locality.
A safe doubly linked list cannot use two `Box`es
Two owners of one node is not `Box`. Use the standard list, indices into a `Vec`, or `unsafe`.
Indexing a list is O(n)
If you write `list.iter().nth(i)` in a loop, you have a quadratic scan. Use a `Vec`.
Interview lists are usually `Option<Box<Node>>`
That type is enough for reverse, merge, and cycle detection with two pointers.