What a linked list is
After this: Say what a pointer, a node and the head are.
On this page · 7 sections · 3 checks
Imagine a treasure hunt in a house. Someone hands you the first clue. Each clue gives you a number to write down, and each clue tells you where the next clue is hidden.
- The clue in your hand says: "Write down 3. The next clue is under the doormat."
- Under the doormat you find: "Write down 7. The next clue is in the blue cup."
- In the blue cup you find: "Write down 9. This is the last clue."
You have written down 3, 7 and 9. Three things about this hunt matter for this tutorial.
- The clues are not next to each other. They can be anywhere in the house.
- The order comes from the clues themselves. Each clue says where to go next.
- You cannot go straight to the third clue. You have to start at the first one and follow the trail.
A program can store a list of values in the same way. This tutorial shows how, and when it is a good idea.
Saying where something is
A running program keeps its data in the computer's . You can picture memory as a very long row of numbered places, like houses along a road. The number of a place is called its .
Look again at the line "the next clue is under the doormat". That line does not contain the next clue. It only says where the next clue is. A program can do the same thing: it can store a small value that says where some other data is in memory. That value is called a .
Python and Java use the word reference for this. In this tutorial the two words mean the same thing. We say pointer because that is the word most people use when they talk about linked lists. In Python and Java you never work with the number inside a reference. You use a reference to reach the data, and the language does the rest.
Nodes
Now replace each clue with a small piece of data in memory. Each piece holds two things: one value, and a pointer to the next piece. This piece of data is called a . We call its two parts value and next.
A chain of nodes joined by pointers is a .
Here is the treasure hunt as three nodes. The addresses are made up. They are in no particular order, because nodes can be anywhere in memory.
| Address | value | next |
|---|---|---|
| 5000 | 3 | 8200 |
| 8200 | 7 | 1600 |
| 1600 | 9 | nothing |
You can read this table the way you followed the clues.
- Start at address 5000. The value is 3, and
nextsays 8200. - Go to address 8200. The value is 7, and
nextsays 1600. - Go to address 1600. The value is 9, and
nextholds nothing, so the list ends here.
The numbers themselves do not matter. Only the place each pointer leads to matters. So from now on we leave out the addresses and draw each pointer as an arrow:
3 → 7 → 9
Where a list starts and ends
You needed the first clue in your hand before the hunt could start. A program needs the same thing. It keeps a pointer to the first node in a variable, and this pointer is called the . If the program loses the head, it loses the list, because nothing else leads to the first node.
The last clue said "this is the last clue". The last node does this with a special value that means "nothing": Nonenull. When a node's next is Nonenull, there are no more nodes.
Watch it happen
Here are the three nodes. The word head and its arrow show the head pointer, and the sign ∅ after the last node means "nothing". The program will search for two values. A comparison is one check of whether a node holds the value you want. Press the button to run each step, and watch how many comparisons each search needs.
- Search for 3Search for 3. It is in the first node, so one comparison is enough.
- Search for 9
Run an operation to begin.
The smallest example in code
In code, a node is a small object. To make objects of our own we write a class, which is a template for making them. A class lists the fields, the variables that every object made from it carries. Our class has two fields.
class Node: def __init__(self, value, next=None): self.value = value # what the node holds self.next = next # where the next node is, or None at the endclass Node { int value; // what the node holds Node next; // where the next node is, or null at the end
Node(int value) { this.value = value; } Node(int value, Node next) { this.value = value; this.next = next; }}Here is how to read it.
Nodeis the name of the class. Every node we make has the same two fields.valueholds the data.nextholds a pointer to the next node. It does not hold a whole node inside it. It only says where the next node is, so a node stays small.- The __init__Node(...) code runs each time a new node is made, and it fills in the fields.
- If you make a node without saying what comes next,
nextstays Nonenull.
Now we make three nodes and link them by hand.
first = Node(3)second = Node(7)third = Node(9)first.next = second # 3 points at 7second.next = third # 7 points at 9head = first # head points at the first nodeprint(head.value, head.next.value, head.next.next.value)3 7 9
Node first = new Node(3);Node second = new Node(7);Node third = new Node(9);first.next = second; // 3 points at 7second.next = third; // 7 points at 9Node head = first; // head points at the first nodeSystem.out.println(head.value + " " + head.next.value + " " + head.next.next.value);3 7 9
Here is what happens, one step at a time.
- The first three lines make three separate nodes. Nothing joins them yet, so every
nextis Nonenull. first.next = secondstores a pointer to the second node inside the first node. Now 3 points at 7.second.next = thirddoes the same for the next pair. Now 7 points at 9.head = firstmakes head point at the first node. The list is 3 → 7 → 9.head.next.valuemeans: go to the node head points at, follownextonce, and read the value there. That is 7. Writing.nexttwice follows two pointers and reaches 9.
Following next is the only way to move along a list.
Check yourself
The list is 3 → 7 → 9. What does the node holding 3 store?
A program sets head to Nonenull by mistake, and no other variable points at any node of the list. How much of the list can it still reach?
These nodes are linked in a different order. What does this print?
first = Node(3)second = Node(7)third = Node(9)first.next = thirdthird.next = secondhead = firstprint(head.value, head.next.value, head.next.next.value)Node first = new Node(3);Node second = new Node(7);Node third = new Node(9);first.next = third;third.next = second;Node head = first;System.out.println(head.value + " " + head.next.value + " " + head.next.next.value);What comes next
You now know what a linked list is. It may look like more work than an array: every value needs a pointer, and you cannot jump to a position. So why would anyone use one? The next lesson answers that question. After that, we learn to go through a list, change it and reverse it.
- Say what a pointer is: a value that says where something is stored in memory.
- Describe a node: one value, plus a pointer to the next node.
- Say what the head is, and how the last node marks the end of the list.