Linked list
A linked list is made of nodes. Each node stores 2 things: its value and a reference, which is the address of the next node. The first is called the head and the last, the tail; the tail points to nothing, written None in Python and null in Java. Since each node says where the next one is, they can be anywhere in memory. The whole list is named by its head: with the first one, you reach them all.
Inserting into a linked list is cheap. To put a node P between A and B, 2 changes are enough: P points to B, and A points to P. Nobody else moves, so it costs O(1), whether there are 10 or 10 million nodes. In an array, on the other hand, inserting in the middle shifts all the ones after. It's like adding a car to a train: you unhook 2 cars, put it in and hook it up, without touching the rest of the train.
Removing a node is even simpler: to take P out of A β P β B, it's enough for A to point to B. It's a single change, O(1). P still points to B, but nobody reaches P from the head, so it's no longer part of the list. It's like leaving a circle holding hands: the people on each side take each other's hands and you're out.
The linked list's weak spot is reaching a position. Since nodes are scattered, there's no calculation that says where number 500 is: you have to start at the head and jump node to node 499 times. Reaching node i costs O(n). Searching for a value is also a linear search. It's like the treasure hunt: for clue 50 you have to follow the 49 before it.
There are 3 common kinds of linked list. Singly linked: each node points to the next and the tail points to nothing. Circular: the tail points back to the head and forms a ring, where any node can act as the start. Doubly linked: each node points to the next and also the previous, so you can walk both ways, at the cost of storing one more reference per node.
Singly linked lists are used to build stacks, queues, hash tables (chaining the ones that collide in the same bucket) and graphs (each node's list of neighbors). Doubly linked ones help when you need to reach the previous and next quickly: browser history or the LRU cache, which evicts the data used longest ago. Circular ones help with things that repeat in rounds, like the operating system giving each program a short turn on the processor, one after another.
