Edexcel International Advanced Level Computer Science, point 14.1.1
Definition
A linked list is a dynamic data structure made of nodes, where each node holds a data item and a pointer (reference) to the next node. The list is reached through a head pointer that points to the first node. The last node's pointer is null (None).
Function and why needed
- It stores a collection that can grow and shrink easily, with no fixed size.
- Inserting or deleting needs only pointer changes, without shifting items as an array does (4.2.1).
- It is the basis of other structures (stacks, queues, hash-table chains, graphs).
a. Structure
- A node has data and a next pointer.
- Nodes are stored anywhere in memory (not next to each other), and linked by their pointers.
- An empty list has
head = None.
Types
| Type | Description |
|---|---|
| Singly linked | Each node points to the next only. You can move forwards only |
| Doubly linked | Each node points to the next and the previous: can move both ways (more memory) |
| Circular | The last node points back to the first |
b. Ways to represent a linked list
Using classes
Each node is an object, and its next attribute holds a reference to another node object.
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
Using references (arrays and indices)
In languages without objects or pointers, a linked list can be held in parallel arrays, with the "pointer" as the index of the next item, and -1 as null:
| Index | 0 | 1 | 2 | 3 |
|---|---|---|---|---|
| Data | 9 | 5 | 14 | |
| Next | 2 | 0 | -1 |
head = 1: so the order is index 1 (5), index 0 (9), index 2 (14): 5, 9, 14.
c. Operations
Traverse (visit every node)
Start at the head, follow next pointers until None.
def traverse(self):
current = self.head
while current is not None:
print(current.data)
current = current.next
Time: .
Search
def search(self, target):
current = self.head
while current is not None:
if current.data == target:
return True
current = current.next
return False
Time: . There is no direct access by index, so a binary search isn't possible.
Insert
At the front (very fast, ):
def insert_front(self, data):
node = Node(data)
# new node points to the old first node
node.next = self.head
self.head = node # head now points to the new node
At the end ( unless a tail pointer is kept):
def insert_end(self, data):
node = Node(data)
if self.head is None:
self.head = node
return
current = self.head
while current.next is not None:
current = current.next
current.next = node
After a given node (pointer changes only):
The order of the two steps matters: doing step 2 first would lose the link to C.
Delete
Find the node, then make the previous node skip it.
def delete(self, target):
previous = None
current = self.head
while current is not None:
if current.data == target:
if previous is None: # deleting the head
self.head = current.next
else:
previous.next = current.next
return True
previous = current
current = current.next
return False # not found
Time: to find the node, then to remove it.
Complete class with a demonstration
ll = LinkedList()
for x in (14, 9, 5):
ll.insert_front(x) # list is now 5 -> 9 -> 14
ll.traverse() # 5, 9, 14
print(ll.search(9)) # True
ll.delete(9)
ll.traverse() # 5, 14
Linked list and array compared
| Linked list | Array | |
|---|---|---|
| Size | Dynamic | Fixed (or costly to resize) |
| Memory layout | Scattered nodes | Contiguous |
| Access by index | (must follow the links) | |
| Insert/delete in the middle | once found (change pointers) | (shift items) |
| Memory per item | More (data plus pointer) | Less |
| Binary search | No | Yes (if sorted) |
| Best for | Frequent inserts and deletes, unknown size | Fast random access, fixed size |
Benefits
- Dynamic size: grows and shrinks as needed.
- Efficient insertion and deletion (no shifting).
- Memory is used only for the items stored.
- Easy to build other structures (stacks, queues).
Drawbacks
- No direct access: reaching item means following pointers.
- Extra memory for pointers.
- Slower to search and traverse than an array (poor cache use as nodes are scattered).
- More complex to code (pointer errors such as losing nodes or creating loops).
- A singly linked list can't move backwards.
Worked example
Task: the list is 10 -> 20 -> 30. Show the list after inserting 15 after 10, and then deleting 20.
Insert 15 after 10:
- New node
15. Set15.next = 10.next(which is20). - Set
10.next = 15. - List:
10 -> 15 -> 20 -> 30.
Delete 20:
- Find 20, keeping track of the previous node (15).
- Set
15.next = 20.next(which is30). - List:
10 -> 15 -> 30.
Number of items moved: none. In an array, inserting 15 would have moved 20 and 30 along, and deleting 20 would have moved 30 back.
Exam tip: draw boxes and arrows and update the pointers in the right order. For insertion, set the new node's pointer first, then redirect the previous node. Mention the head pointer and null. For comparisons with arrays, say linked lists are dynamic, with easy insertion and deletion, but slow access by position.
Check yourself
What does each node of a singly linked list contain?
A data item and a pointer to the next node (null for the last node).
What is the advantage of a linked list over an array for insertion?
Items needn't be shifted: only pointers are changed, so insertion in the middle is quick once the position is found.
Why can't you do a binary search on a linked list?
There is no direct access to the middle item; you can only reach nodes by following pointers from the head.
In what order should the pointers be changed when inserting a node B between A and C?
First set B's next to C (A's old next), then set A's next to B; reversing them would lose the link to C.
What is stored in an empty linked list's head?
Null (None).