Simple Linked List
Simple Linked List

Simple Linked List



You work for a music streaming company.

You've been tasked with creating a playlist feature for your music player application.


Write a prototype of the music player application.

For the prototype, each song will simply be represented by a number. Given a range of numbers (the song IDs), create a singly linked list.

Given a singly linked list, you should be able to reverse the list to play the songs in the opposite order.


The linked list is a fundamental data structure in computer science, often used in the implementation of other data structures.

The simplest kind of linked list is a singly linked list. That means that each element (or "node") contains data, along with something that points to the next node in the list.

If you want to dig deeper into linked lists, check out this article that explains it using nice drawings.

Do not implement the struct SimpleLinkedList as a wrapper around a Vec. Instead, allocate nodes on the heap.

This might be implemented as:

pub struct SimpleLinkedList<T> {
    head: Option<Box<Node<T>>>,

The head field points to the first element (Node) of this linked list.

This implementation also requires a struct Node with the following fields:

struct Node<T> {
    data: T,
    next: Option<Box<Node<T>>>,

data contains the stored data, and next points to the following node (if available) or None.

Why Option<Box<Node<T>>> and not just Option<Node<T>>?

Try it on your own. You will get the following error.

| struct Node<T>
| ^^^^^^^^^^^^^^ recursive type has infinite size
|     next: Option<Node<T>>,
|     --------------------- recursive without indirection

The problem is that at compile time the size of next must be known. Since next is recursive ("a node has a node has a node..."), the compiler does not know how much memory is to be allocated. In contrast, Box is a heap pointer with a defined size.

Edit via GitHub The link opens in a new window or tab
Rust Exercism

Ready to start Simple Linked List?

Sign up to Exercism to learn and master Rust with 96 exercises, and real human mentoring, all for free.