Linked lists in practice
After this: Pick a linked list when it wins, and use deque and LinkedList with their costs in mind.
On this page · 5 sections · 3 checks
So far you have built every list by hand. In real programs you rarely need to, because Python and Java give you something ready to use. This lesson shows what they give you and when to use it. Quite often, the right choice turns out to be an array.
What Python and Java give you
Python has no class called a linked list. The closest thing in its standard library is collections.deque. A , said like "deck", is short for double-ended queue. It is a list built to be fast at both ends.
Java has java.util.LinkedList, a doubly linked list with a head and a tail. It can be used as a Deque, which is Java's name for the same idea.
from collections import deque
d = deque([3, 5, 7])d.appendleft(1) # O(1) at the front: a list would shift every elementd.append(9) # O(1) at the backd.popleft() # O(1); list.pop(0) is O(n)print(d, d[0], d[-1]) # the ends are O(1); d[i] in the middle is a walkdeque([3, 5, 7, 9]) 3 9
Deque<Integer> d = new LinkedList<>(List.of(3, 5, 7));d.addFirst(1); // O(1) at the front: an ArrayList would shift every elementd.addLast(9); // O(1) at the back: LinkedList keeps a taild.pollFirst(); // O(1); ArrayList.remove(0) is O(n)System.out.println(d + " " + d.peekFirst() + " " + d.peekLast()); // a Deque has no get(i); LinkedList's get(i) is a walk[3, 5, 7, 9] 3 9
Here is what the code does, starting from 3, 5, 7.
- Add 1 at the front. The deque is 1, 3, 5, 7.
- Add 9 at the back. The deque is 1, 3, 5, 7, 9.
- Remove from the front. The 1 goes, and the deque is 3, 5, 7, 9.
- Print the whole deque, then its first value, then its last value.
Here is what each job costs.
| Job | Python deque | Java LinkedList | Cost |
|---|---|---|---|
| Add at the front | d.appendleft(x) | d.addFirst(x) | O(1) |
| Add at the back | d.append(x) | d.addLast(x) | O(1) |
| Remove from the front | d.popleft() | d.pollFirst() | O(1) |
| Remove from the back | d.pop() | d.pollLast() | O(1) |
| Read by position | d[i] | list.get(i) | O(n): a walk |
| Find a value | x in d | d.contains(x) | O(n) |
The last two rows are the ones to remember. Both ends are cheap, and reaching anything in the middle takes a walk, whether you go by position or by value. In Java, get needs a variable of type LinkedList, because Deque has no get.
When a linked list wins
There are three situations, and all of them avoid the walk.
- Adding and removing at both ends. A queue is the usual example: jobs join at the back and leave from the front.
dequeandLinkedListare made for this. - Changing the list next to a node you hold. If something else already gives you the node, you can insert or remove next to it in O(1). The classic example is the LRU cache from the last lesson. A hash map, a structure that has its own tutorial, finds the node by its key. The doubly linked list then moves that node to the front.
- Never copying. Each node is created on its own, so nothing has to move to make room. A dynamic array, such as a Python
listor a JavaArrayList, sometimes has to copy everything when it grows.
When the array wins anyway
Notation such as O(1) and O(n) is called . It counts how the steps grow, but it does not say how long one step takes. So two jobs with the same big-O can run at very different speeds.
Going through every item is O(n) in both an array and a linked list. The array is usually much faster, and the reason is how the processor reads memory.
When the processor reads one place in memory, it also brings in the places around it, as one block. This block is called a cache line. The processor keeps recent blocks in a small, fast store called the cache.
- Array cells sit side by side. Reading one cell brings its neighbours into the cache, so the next few reads are very fast.
- Nodes can be anywhere. Each
nextoften leads to a place that is not in the cache, so the processor has to wait for it.
Each node also stores a pointer, so a linked list takes more memory for the same values.
So for reading by position, going through every item, sorting and most everyday lists, the array (list in Python, ArrayList in Java) is faster in practice. This is true even where the big-O is the same, and the gap is much bigger where it is not.
Check yourself
You keep a work queue: jobs are added at the back and taken from the front, and nothing is ever read by position. What fits best?
What does this print?
from collections import deque
d = deque([3, 5, 7])d.appendleft(1) # O(1) at the front: a list would shift every elementd.append(9) # O(1) at the backd.popleft() # O(1); list.pop(0) is O(n)print(d, d[0], d[-1]) # the ends are O(1); d[i] in the middle is a walkDeque<Integer> d = new LinkedList<>(List.of(3, 5, 7));d.addFirst(1); // O(1) at the front: an ArrayList would shift every elementd.addLast(9); // O(1) at the back: LinkedList keeps a taild.pollFirst(); // O(1); ArrayList.remove(0) is O(n)System.out.println(d + " " + d.peekFirst() + " " + d.peekLast()); // a Deque has no get(i); LinkedList's get(i) is a walkGoing through every element is O(n) for both an array and a linked list. Why is the array usually faster in practice?
Connect
- Choose by the jobs your program does most: arrays for reading by position and going through every item, linked lists for both ends and for changes next to a node you hold.
- Use Python's deque and Java's LinkedList with their costs in mind.
- Say why the same big-O does not mean the same speed: array cells sit side by side in memory, and nodes do not.