liveBook · Manning

Linked list

As explained in Elixir in Action, Third Edition

A singly linked list is a sequence of nodes in which each node refers to the next node and traversal proceeds in one direction.

A singly linked list is a sequence of nodes, where each node contains a value and refers to the next node. Elixir lists use this recursive structure, so traversal proceeds from the head toward the tail. section 2.4.4

The representation makes some operations natural and others expensive:

  • Prepending uses the cons form [head | tail] and adds an element without traversing the existing list. section 2.4.4
  • Finding an element, calculating length, accessing by index, replacing, inserting, appending, and concatenating generally require traversal and have O(n) complexity. section 2.4.4
  • Choose tuples, maps, or a higher-level data structure when direct access is required. section 2.4.4

Decomposing and extending a list

Pattern matching exposes the first element and the remainder: a_list = [head | tail]. The same structure supports construction, as in [:new_element | a_list], which prepends without mutating the original binding.

Figure 2.1 Recursive structure of the list [1, 2, 3, 4]
CH01_F02_Juric3

View the recursive structure of [1, 2, 3, 4] as nested head-and-tail links. section 2.4.4

How Elixir in Action, Third Edition covers it

How deep it goes
Explains recursive representation, construction, traversal costs, and operation trade-offs, while deferring detailed List-module reference.
Examples
Demonstrates prime-number lists, indexed access, membership, replacement, insertion, concatenation, and head-tail decomposition.
What you can do after
Apply it via List and Enum operations while recognizing when sequential access makes another data structure more suitable.
How it connects
Connects lists with arrays, tuples, maps, enumerables, and the List and Enum modules.

Also explained in

Compare how 8 books explain linked list →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Elixir in Action, Third Edition. The section links point to the parts of the book it draws on.