Linked Lists (Cambridge International A-Level Computer Science)
Exam board Cambridge International · A-LevelUpdated 2025-06-03
Linked lists
How do you program a linked list?
-
To recap the main operations of linked lists and how they work, read our ADT Overview page
-
To program a Linked List you must be able to perform the following operations:
- Create a linked list
- Traverse a linked list
- Add data to a linked list
- Remove data from a linked list
Create a linked list
- Define the class called ‘Node’ that represents a node in the linked list.
- Each node has 2 fields ‘fruit’ (this stores the fruit value) and ‘next’ (this stores a reference to the next node in the list)
- We create 3 nodes and assign fruit values to each node
- To establish a connection between the nodes we set the ‘next’ field of each node to the appropriate next node
| Pseudocode |
|---|
| Python |
|---|
| Java |
|---|
Algorithm to traverse a linked list
- We traverse the linked list starting from ‘node1’. We output the fruit value of each node and update the ‘current’ reference to the next node until we reach the end of the list
| Pseudocode |
|---|
| Python |
|---|
# Traverse the linked listcurrent = node1while current is not None: print(current.fruit) current = current.next |
| Java |
|---|
// Traverse the linked listcurrent = node1;while (current != null) {System.out.println(current.fruit); current = current.next; |
Algorithm to add data to a linked list
- We create a new node with the new data and check if the list is empty
- If it isn’t, we find the last node and add the new node to the end
| Pseudocode |
|---|
| Python |
|---|
# Add "orange" to the end of the linked listnew_node = Node("orange")if node1 is None: # If the list was empty, make "orange" the head node1 = new_nodeelse: # Find the last node and append "orange" to it current = node1 while current.next is not None: current = current.next current.next = new_node |
| Java |
|---|
// Add "orange" to the end of the linked listNode new_node = new Node("orange");if (node1 == null) {// If the list was empty, make "orange" the headnode1 = new_node;} else {// Find the last node and append "orange" to it current = node1; while (current.next != null) { current = current.next; } current.next = new_node;} |
Algorithm to remove data from a linked list
- We traverse the list until we find the data to be removed
- If the data to be removed is the first node, we update the head, else we update the node before to bypass the data to be removed
| Pseudocode |
|---|
| Python |
|---|
# Find and remove "apple"while current is not None: if current.fruit == "apple": if previous is None: # If "apple" is the first node, update the head node1 = current.next else: # Remove the node by updating the previous node's next pointer previous.next = current.next break previous = current current = current.next |
| Java |
|---|
// Find and remove "apple" while (current != null) { if (current.fruit.equals("apple")) {if (previous == null) { // If "apple" is the first node, update the head node1 = current.next; } else { // Remove the node by updating the previous node's next pointer previous.next = current.next; } break; }previous = current; current = current.next; } |
Worked Example The following diagram represents an Abstract Data Type (ADT) for a linked list.
The free list is as follows:
Explain how a node containing data value B is added to the list in alphabetic sequence.[4]
Final answer: Answer
One mark per point
- Check for a free node [1 mark]
- Search for correct insertion point [1 mark]
- Assign data value B to first node in free list / node pointed to by start pointer of free list [1 mark]
- Pointer from A will be changed to point to node containing B (instead of C) [1 mark]
- Pointer from B will be changed to point to node containing C [1 mark]
- Start pointer in free list moved to point to next free node [1 mark]
Was this revision note helpful?
Build on this topic

