19.1 What is a Linked List?
An array keeps items in fixed neighbouring slots, so inserting in the middle means shuffling everything along. A linked list solves that. Each item sits in a node that also stores a pointer to where the next item is. To insert or remove an item you just change a couple of pointers — the data itself never moves.
Think of a treasure hunt: each clue tells you where the next clue is hidden. You start at the first clue (the start pointer) and follow the trail until a clue says “this is the last one” (a null pointer). Re-routing the hunt is easy — you only rewrite which clue points where.
Since Python has no built-in pointer type, we simulate the list with two arrays: a data array holding the values and a pointer array holding, for each node, the index of the next node. We also keep a free list of unused nodes.
| Term | Meaning |
|---|---|
| Linked list | A chain of nodes where each node points to the next. Reordered by changing pointers, not by moving data. |
| Node | One element: a data value plus a pointer to the next node. |
| Pointer / next field | The index of the next node in the chain. |
| Null pointer | A special value (we use -1) marking the end of the list — there is no next node. |
| Start pointer | The index of the first node. -1 means the list is empty. |
| Free list | A chain of unused nodes, ready to be taken when inserting. A free pointer marks its start. |
| Traverse | To step through the list from the start, following each pointer in turn. |
| Dynamic structure | A structure that can grow and shrink at run time by adding/removing nodes. |
- Each node holds data + a pointer to the next node.
- A start pointer marks the first node; the last node holds a null pointer.
- Insert and delete by changing pointers, not by moving data.
Each node is [ data | next pointer ]. Follow the arrows from start.
last node → null (Ø = -1)
- Step 1: What a node contains.
- Step 2: How the list is entered and how it ends.
- Step 3: How items are added/removed.
- Think about what an array has to do to make room.
- The phrase examiners reward is “data is added/removed by manipulating pointers, not by moving data,” plus “the last node has a null pointer.”
- Naming the free list for unused nodes often earns an extra mark.
19.1 What is a Linked List?
19.2 Two Arrays and the Free List
- Mark scheme: “Complete the diagram to show how the linked list is represented using the variables and arrays” is a near-guaranteed Paper 2/3 question (e.g. 9618/22 O/N 2023 Q3).
- The marks come from reading the start pointer, following the pointer array, and recognising the free list.
- Master the table and you've cleared the most common linked-list question.
We store the list in two arrays of the same length. The data array holds the values. The pointer array holds, at the same index, the location of the next node. Two variables complete the picture: start (index of the first node) and free (index of the first unused node). Unused nodes are chained together exactly like the list — that chain is the free list.
MAX = 7
data = [None] * MAX # the values
nextp = [None] * MAX # index of the next node
start = -1 # -1 = empty list
free = 0 # first free node
def setup():
global start, free
start = -1
free = 0
for i in range(MAX - 1):
nextp[i] = i + 1 # each free node links to the next
nextp[MAX - 1] = -1 # last free node -> nullHere is a list of three marks — 60, 45, 88 — stored in the arrays. The start pointer says the first node is at index 2; from there you follow the pointer array.
| Index | data | nextp | role |
|---|---|---|---|
| 0 | 88 | -1 | 3rd node → null |
| 1 | 45 | 0 | 2nd node → index 0 |
| 2 | 60 | 1 | 1st node → index 1 |
| 3 | · | 4 | free → 4 |
| 4 | · | 5 | free → 5 |
Read the list by starting at index 2 (60), following nextp: 2 → 1 → 0 → null gives 60, 45, 88. The free list begins at index 3. Pointers: start = 2, free = 3.
- Step 1 — follow start: start = 2 → data 60, nextp 1.
- Step 2 — follow the chain: 1 → data 45, nextp 0; 0 → data 88, nextp -1 (end).
- Start at index 1, follow each nextp value.
19.2 Two Arrays and the Free List
19.2B Interactive Simulator
Try the linked list yourself. Set MAX_SIZE, enter a value, and click Insert Front, Find, or Delete. Step through the pointer moves one at a time, or press Play to watch them run automatically. The free list starts pre-linked — every insert takes a node from it, every delete returns one to it.
Two-Array Representation
| Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
| data | · | · | · | · | · | · | · |
| nextp | 1 | 2 | 3 | 4 | 5 | 6 | -1 |
| ptr | free |
List chain (follow start)
null (empty)
Status: Choose an operation to begin. The free list is pre-linked.
Code (select an op)
1# Choose Insert, Find, or Delete to see the code highlighted here.19.3 Traverse and Find
- Finding an item is the syllabus's named operation for linked lists.
- The technique — start at
start, follownextpuntil you match or hit null — is the same loop you'll reuse for delete, so it's worth getting fluent.
To find a value, set a current pointer to start and step along: if the current node's data matches, stop; otherwise move current to nextp[current]. When current becomes -1 you've run off the end without finding it.
def find(target):
current = start
while current != -1: # until we run off the end
if data[current] == target:
return current # found - give its index
current = nextp[current] # follow the pointer
return -1 # not found- Start at
start; whilecurrent != -1, check the data, then move tonextp[current]. - The null pointer is what stops the loop.
- Step 1 — start at the start: current = start.
- Step 2 — loop until null: while current != -1.
- Step 3 — match or move on: return True on a match, else follow the pointer; return False after the loop.
- This is the original find; return the index, not True/False.
- The loop condition must be
current != -1(the null check), not a count. - Using the null pointer to stop is the whole idea of a linked list, and examiners look for it specifically.
19.3 Traverse and Find
19.4 Insert a Node
- Insert is a named operation.
- The marks reward the four pointer moves: take a node off the free list, store the data, point the new node at the rest of the list, and update
start. - Inserting at the front is the simplest case and the one most often asked.
To insert a value at the front: take the first free node, store the data there, make the new node point to the old start, then move start to the new node. The free pointer moves on to the next free node.
def insert_front(item):
global start, free
if free == -1: # no free nodes left
print("List full")
else:
new = free # take a free node
free = nextp[free] # free list moves on
data[new] = item # store the data
nextp[new] = start # new node points to old start
start = new # start now points to new node- Take node from free list → advance free → store data → new node's pointer = old start → start = new node.
- Never lose the free list.
- Five moves: check free empty; take node and advance free; store data; set new node's pointer to old start; update start.
- Same five moves, different names.
19.4 Insert a Node
19.5 Delete a Node
- Deleting is the third named operation.
- The idea: find the node, make the previous node point past it, then hand the freed node back to the free list.
- No data is moved — only pointers change.
We walk the list keeping track of the previous node as well as the current one. When we find the target, the previous node's pointer is set to skip over it. If the target is the very first node, we just move start instead. Finally we link the removed node onto the front of the free list so it can be reused.
def delete(target):
global start, free
current = start
previous = -1
while current != -1 and data[current] != target:
previous = current
current = nextp[current]
if current == -1:
return False # not found
if previous == -1:
start = nextp[current] # delete the first node
else:
nextp[previous] = nextp[current] # bypass the node
nextp[current] = free # return node to free list
free = current
return True- Find the node (tracking previous).
- Point previous past it (or move start if it's first).
- Then attach the freed node to the free list.
- The data stays put; only pointers change.
- current = 2; visit 2, follow nextp to 0; visit 0, follow nextp to 4; visit 4, follow nextp to -1 (stop).
- Start at 3, follow each pointer to null.
- The list is empty exactly when start is the null pointer.
- Traverse and add one each step.
19.5 Delete a Node
19.6 Full Exam-Style Question
- A program at moshikur.com stores a linked list of integers using a data array
data, a pointer arraynextp, astartpointer and afreepointer. - Null pointers are stored as -1.
(a) The pointer array contains: index 0 → 4, index 2 → 0, index 4 → -1. The data array contains: 0 = 30, 2 = 12, 4 = 75. The start pointer is 2. State the order in which the integers are read from the list. [2]
(b) Write program code for a function find(target) that traverses the list and returns the index of the node holding target, or -1 if it is not in the list. [4]
(c) Write program code for a procedure insert_front(value) that inserts a value at the front of the list, taking a node from the free list, or outputs "List full" if no free node exists. [5]
- (a) follow start = 2 through the pointers to null.
- (b) current = start; while current != -1; compare and return index; follow nextp and return -1.
- (c) check free empty + message; take free node; advance free; store value + point at old start; set start = new.
19.6 Full Exam-Style Question
✓ Key Points Summary
19.7 Practice Tasks
Fifteen exam-style tasks. Click Hint for bullet-point guidance, then Help to reveal a worked Python solution.
Question Bank
Answer all questions, then press Submit Quiz to see your score.
Question 1Multiple Choice
A linked list is best described as:
Question 2Multiple Choice
What does a null pointer (-1) represent in a linked list?
Question 3Multiple Choice
Which two arrays simulate a linked list in Python?
Question 4True / False
The start pointer stores the index of the first node in the list.
Question 5Multiple Choice
What is the free list?
Question 6Multiple Choice
In find(target), what is the correct loop condition?
Question 7Multiple Choice
In insert_front(item), what is the correct order of operations?
Question 8Multiple Choice
When deleting the first node (previous == -1), how is the node removed?
Question 9True / False
When deleting a node, you must move all the data after it to fill the gap.
Question 10Multiple Choice
After bypassing a deleted node, how do you return it to the free list?
Question 11Multiple Choice
A linked list is described as a "dynamic structure" because:
Question 12Multiple Choice
Given start = 2, nextp[2] = 1, nextp[1] = 0, nextp[0] = -1, and data[2] = 60, data[1] = 45, data[0] = 88, what is the read order?
Answer all 12 questions to enable submission.