3Linked Lists
Introduction to linked lists
Arrays keep their elements side by side in memory and reach any element instantly by its index. But inserting in the middle or growing the array is costly. A linked list solves both problems with a different layout. This lesson covers the idea behind linked lists, their three kinds and how they compare with arrays.
- Which operations are hard for an array and how a linked list handles them
- The idea of nodes and links, and how singly, doubly and circular lists differ
- Which operations suit an array and which suit a linked list
1Two limits of the array
An array keeps its elements in contiguous memory. The address of the fourth element is computed from the address of the first, so every element is reached in one step.
The same layout brings two problems. First, to insert at the front or in the middle, every element behind that spot moves one place; in an array of n elements that is O(n) work. You can see this shifting in Inserting into an array.
Second, the size of the array is fixed up front. When it fills up, a bigger block is allocated and every element is copied over. Dynamic arrays do this for you, but the copying has a cost.
2Nodes and links
A linked list does not put its elements side by side. Each element lives in its own node, and next to the value the node holds the address of the next node. That address is called a link; a link is a pointer. head points to the start of the list, and the last node's link is NULL.
Inserting a new element moves no elements: the new node is allocated anywhere in memory and a few links change. The list also grows one node at a time; there is no size to fix in advance.
There is a price: the nodes are not side by side, so you cannot jump to the 3rd element. You start at the front and follow the links one by one.
3Three kinds of linked list
Singly linked list
Each node knows only the node after it. You can only move forward. It is the simplest kind, and the first part of this section is about it.
Doubly linked list
Each node knows both the next node (next) and the previous one (prev). You can move in both directions, and deleting a node does not require searching for the one before it. In return every node carries one extra pointer.
Circular linked list
The last node points to the first node instead of NULL, so the list forms a ring. Work that goes round in turns, such as passing the turn between players in a game, fits this shape well. When traversing you must know where to stop, because NULL never comes.
4Array or linked list?
Both do the same job: they hold a sequence of elements. Which one fits depends on the operation you perform most often.
| Operation or property | Array | Singly linked list |
|---|---|---|
| Access by index (k-th element) | O(1) | O(n): walk from the front |
| Insert at the front | O(n): every element shifts | O(1) |
| Insert at the end | O(1) if there is room | O(n); O(1) with a tail pointer |
| Size | Fixed up front | Grows as nodes are added |
| Extra memory | None | One pointer per node |
| Cache friendliness | Good: elements side by side | Poor: nodes scattered |
The last row matters in practice. When the processor reads a value from memory, it also brings the values next to it into the cache. In an array the next element is usually already there; in a linked list the next node may be somewhere else in memory. So of two traversals with the same Big-O, the array one usually finishes faster.
In short: if you mostly reach elements by index, use an array; if you often insert and delete at the front or in the middle, a linked list fits better.
5What this section covers
- Singly linked list: definition, creating, traversal, inserting at the head, at the end and at a position, three deletions, search and reversing.
- Doubly linked list: definition, insertion and deletion, traversing in both directions.
- Circular linked list: definition, insertion, deletion and where the loop ends.
Every operation lesson has a step-by-step drawing, code in C, Java and Python, and the complexity of the operation. The starting point is What is a singly linked list?.
6Try it
Why is inserting at the front of an array of 1000 elements costly?
The answer is B. In an array the elements must sit side by side; to make room at the front, all of them move. In a linked list only a few links change, see Inserting at the head.
Show the answer
The answer is B. In an array the elements must sit side by side; to make room at the front, all of them move. In a linked list only a few links change, see Inserting at the head.
In which operation is an array clearly faster than a linked list?
The answer is B. In an array the address of the k-th element is computed, which takes O(1). In a linked list you walk k steps from the front to reach the k-th node.
Show the answer
The answer is B. In an array the address of the k-th element is computed, which takes O(1). In a linked list you walk k steps from the front to reach the k-th node.
Lessons you mark show up in the curriculum. This is kept on this device only.