Why not an array
After this: Say what a linked list avoids and what it pays instead.
On this page · 8 sections · 3 checks
You know what a linked list is: nodes that each point at the next one. That raises a fair question. An array already stores a list of values, so why would anyone want something else?
The answer starts with a job that arrays do badly.
The job arrays do badly
Take the array [10, 20, 30, 40, 50] and add 5 at the front. Index 0 is already taken, so room has to be made.
- Move each of the five values one cell to the right, starting with 50.
- Write 5 at index 0.
Five values mean five moves. A million values mean a million moves. This cost is written O(n), which means the work grows with the size of the list. The letter n stands for the number of values. The lesson Counting steps explains O(1) and O(n) in full. In short, O(n) work grows with the list, and O(1) work stays the same however long the list is.
Adding a value near the front of a long array is slow, because everything after that place has to move. Inserting and deleting shows this step by step. What if nothing had to move?
Adding a clue to the hunt
Go back to the treasure hunt. The first clue says "the next clue is under the doormat". You want to add a new clue straight after the first one, and you hide it in a drawer.
- Write the new clue. Copy onto it the line "the next clue is under the doormat".
- Change the first clue so that it says "the next clue is in the drawer".
You edited two lines, and no clue moved. The clues under the doormat and in the blue cup stayed where they were. This works the same way for a hunt of three clues or three thousand.
Adding a node
Nodes work like clues. Take the list 10 → 20 → 30 → 40 → 50 and add 35 after 30.
- Create a new node holding 35. It can be anywhere in memory.
- Point the new node at 40, the node that 30 points at now.
- Point 30 at the new node.
The list is now 10 → 20 → 30 → 35 → 40 → 50. Steps 2 and 3 each change one pointer, and we call that a pointer write. So the insert takes two pointer writes, and no node moved.
The list could have three nodes or three million, and it would still take two pointer writes. This is O(1): the work does not grow with the list. The order of the two writes matters, and a later lesson explains why.
Adding at the front
The front of a list is the best place of all, because head already points there. Adding at the front is called prepending. To add 5 at the front:
- Create a new node holding 5, and point it at the current first node.
- Move head to the new node.
That is two pointer writes again. Here is the same job on an array and on a linked list.
nums = [10, 20, 30, 40, 50]nums.insert(0, 5) # every cell shifts one step right: O(n)
head = Node(10, Node(20, Node(30, Node(40, Node(50)))))head = Node(5, head) # one new node, two pointer writes, nothing shifts: O(1)print(nums[0], head.value, head.next.value)5 5 10
List<Integer> nums = new ArrayList<>(List.of(10, 20, 30, 40, 50));nums.add(0, 5); // every cell shifts one step right: O(n)
Node head = new Node(10, new Node(20, new Node(30, new Node(40, new Node(50)))));head = new Node(5, head); // one new node, two pointer writes, nothing shifts: O(1)System.out.println(nums.get(0) + " " + head.value + " " + head.next.value);5 5 10
Here is how to read it.
- The first two lines use the array. Adding at index 0 moves every value along.
- The line that starts head = Node(10,Node head = new Node(10, builds the whole list in one line. Read it from the inside out. Node(50)new Node(50) is made first. The node for 40 is made next, with the 50 node as its
next, and so on out to 10. The second thing inside the brackets is always the node'snext. - head = Node(5, head)head = new Node(5, head) is the two steps above, in one line. The right side runs first, so it makes a new node whose
nextis the old first node. Then head moves to that new node.
What a linked list pays
A linked list gives something up in return. An array can calculate the address of any index, because its cells sit side by side and have the same width. Nodes can be anywhere, so a linked list has nothing to calculate with.
To reach the fifth node, you start at the head and follow next four times. To reach the thousandth node, you follow it 999 times. Following pointers from node to node like this is called a walk, and a walk is O(n).
So an insert has two parts: finding the place, and changing the pointers. Changing the pointers is O(1). Finding the place may take a walk. The moving has gone away, but the work of finding the place has not.
The exception is when your program already has a variable pointing at the node. We say that you hold the node. If you hold a node, you need no walk to insert after it.
| Job | Array | Linked list |
|---|---|---|
| Read the value at a position | O(1): calculate the address | O(n): walk from the head |
| Add at the front | O(n): every value moves | O(1): two pointer writes |
| Add right after a node you hold | O(n): the values after it move | O(1): two pointer writes |
| Find a value | O(n): look at each cell | O(n): walk from the head |
Watch it happen
Start from 10 → 20 → 30 → 40 → 50. Watch for the walk, and for the moment each arrow changes.
- Insert 35 after 30Insert 35 after 30. Watch the walk to 30, then the two pointer writes. No node moves.
- Prepend 5
- Search for 40
Run an operation to begin.
Reaching 30 visited three nodes, and the insert then made two pointer writes. The prepend made two pointer writes and needed no walk, because head is a pointer we already hold. By then the list had seven nodes, and the search for 40 made six comparisons, one for each node up to and including 40.
Check yourself
You hold a pointer to the node 30 in a linked list of a million nodes. What does inserting 35 right after it cost?
You want to insert at position i of a linked list with n nodes, and you do not hold any node yet. Where does the work go?
What does this print?
nums = [10, 20, 30, 40, 50]nums.insert(0, 5) # every cell shifts one step right: O(n)
head = Node(10, Node(20, Node(30, Node(40, Node(50)))))head = Node(5, head) # one new node, two pointer writes, nothing shifts: O(1)print(nums[0], head.value, head.next.value)List<Integer> nums = new ArrayList<>(List.of(10, 20, 30, 40, 50));nums.add(0, 5); // every cell shifts one step right: O(n)
Node head = new Node(10, new Node(20, new Node(30, new Node(40, new Node(50)))));head = new Node(5, head); // one new node, two pointer writes, nothing shifts: O(1)System.out.println(nums.get(0) + " " + head.value + " " + head.next.value);Connect
- Say what a linked list avoids: no values move when you insert or delete.
- Say what it pays instead: it cannot calculate where a position is, so reaching a position is a walk.
- Insert after a node you hold with two pointer writes, which is O(1).