How is a linked list stored in memory?
A linked list is not stored in memory as a single contiguous block, like an array. It consists of separate nodes, which can be located in different places in RAM, but are connected to each other by references (pointers).
1. What the structure looks like
Each node stores:
- data (for example, a number, string, object),
- a pointer (reference) to the next node, and sometimes to the previous one.
Example for a singly linked list:
[data | next] → [data | next] → [data | None]In memory, this might look like this (conditional addresses):
Address 1000: [10 | next = 2056]
Address 2056: [20 | next = 3172]
Address 3172: [30 | next = None]The nodes are scattered, but the "chain" is created using pointers (next).
2. Why the nodes are not sequential
Memory is allocated dynamically (via malloc, new, or similar mechanisms).
The system gives a free region, which can be anywhere.
So the next element of the list can be located at a completely different address.
3. How the program "finds" elements
- The list has a reference to the first element (
head). - From
head, the program takes the address of the next node from thenextfield. - Then from that node, another address, and so on along the chain, until it reaches
None.
head → node1 → node2 → node3 → ...4. For a doubly linked list
Each node stores two pointers:
[prev | data | next]This allows movement in both directions (forward and backward).
5. Visually
+------+ +------+ +------+
| 10 |•---->| 20 |•---->| 30 |X
+------+ +------+ +------+
(addresses could be 1000, 2056, 3172)6. The key feature
- Unlike an array, where elements "lie next to each other," in a list the logical sequence does not match the physical layout.
- This makes the list flexible (you can add and remove elements without shifting), but reduces efficiency for random access (O(n)).
Summary:
A linked list is stored in memory as a set of separate nodes, each of which contains data and references to other nodes. These references form a logical chain, even if the nodes are physically scattered across memory.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.