28.1 Introduction: The Linked List ADT
A linked list stores a sequence of items where each item, called a node, holds two things: the data and a pointer (a reference) to the next node. A separate head (or start) pointer marks the first node, and the final node's pointer is the null pointer, signalling the end.
Unlike an array, the nodes are not stored in one contiguous block. To insert or delete you simply change a couple of pointers — no shuffling of data. That makes a linked list ideal when the collection grows and shrinks a lot, such as the waiting list at a coaching centre.
| Term | Meaning |
|---|---|
| Node | One element: a data field plus a pointer to the next node. |
| Head / Start pointer | Points to the first node of the list. |
| Next pointer | The link from one node to the following node. |
| Null pointer | Marks the end of the list (shown as Ø, None, -1 or 0). |
| Free list | A linked list of unused nodes, ready to be reused. |
| Free pointer | Points to the first available free node. |
| Traverse | Visit each node in turn by following the next pointers. |
| Dynamic structure | Grows and shrinks at run time without moving data. |
| Pointer manipulation | Inserting/deleting by changing links, not by copying data. |
28.2 Nodes, the Head & the Null Pointer
- Every linked-list answer rests on one picture: boxes (nodes) joined by arrows (pointers), starting at the head and ending at null.
- If you can draw it, you can describe it and code it.
A linked list: head → [10|●] → [20|●] → [30|●] → Ø
Each node carries data and a pointer to the next. The last node points to null.
28.2 Nodes, the Head & the Null Pointer
28.3 The Node and LinkedList Classes
- Python's object references give the cleanest first model: a
Nodeobject literally holds a reference to the nextNode. - This is the version you will run and test.
class Node:
def __init__(self, data):
self.data = data # value stored
self.next = None # link to next node
class LinkedList:
def __init__(self):
self.head = None # empty list
def append(self, data):
new_node = Node(data)
if self.head is None:
self.head = new_node
else:
current = self.head
while current.next: # walk to the last node
current = current.next
current.next = new_node
def display(self):
current = self.head
while current:
print(current.data, end=" -> ")
current = current.next
print("None")
def search(self, key):
current = self.head
while current:
if current.data == key:
return True
current = current.next
return False
def delete(self, key):
current = self.head
previous = None
while current and current.data != key:
previous = current
current = current.next
if current is None:
return "Node not found"
if previous is None: # deleting the head
self.head = current.next
else:
previous.next = current.next # bypass the node
# after append(10), append(20), append(30)
# 10 -> 20 -> 30 -> None
# search(20) -> True search(40) -> False
# after delete(20)
# 10 -> 30 -> None- Node: Two fields: data and next. A new node always starts with next = None.
- append: If the list is empty, the new node becomes the head. Otherwise walk to the last node (the one whose next is None) and link the new node there.
- display: Start at the head, print each node's data, follow next until None.
- search: Same traversal, but stop and return True the moment the data matches; return False if you reach the end.
- delete: Walk forward keeping a previous reference. To remove a node, point previous.next past it. Special case: deleting the head just moves head forward.
28.3 The Node and LinkedList Classes
28.4 The Array Model & the Free List
- Examiner focus: Paper 3 and Paper 4 almost always present a linked list as two 1D arrays — a
Dataarray and aNextPointerarray — with aStartPointerand aFreePointer. - You must be able to read the table, follow the pointers, and update them after an insert or delete.
Because exam languages may not have objects, a linked list is simulated with parallel arrays. The index of a node is its “address”. StartPointer holds the index of the first node; each NextPointer holds the index of the following node; the null pointer is a special value (this page uses -1; some papers use 0). Unused indices form the free list, headed by FreePointer.
| Index | Data | NextPointer |
|---|---|---|
| 0 | "Mehrin" | -1 |
| 1 | "Adri" | 2 |
| 2 | "Mohar" | 0 |
| 3 | free | 4 |
| 4 | free | -1 |
StartPointer = 1, FreePointer = 3. List order: Adri(1) → Mohar(2) → Mehrin(0) → Ø. Free list: 3 → 4 → Ø. Null pointer = -1. Follow StartPointer=1: index 1 → 2 → 0 → -1 (end).
- Watch the null-pointer convention the question gives you.
- Some papers use
0for null (so valid indices start at 1); others use-1. - Always re-read the stem — do not assume.
28.4 The Array Model & Free List
28.5 Traversing & Searching
- Examiner focus: A classic Paper 3 task gives a half-written
SearchListfunction and asks you to complete it. - The marks are for starting at the start pointer, looping while not null, comparing the data, and advancing via the next pointer.
FUNCTION SearchList(Item) RETURNS INTEGER
NullPtr <- -1
NowPtr <- StartPointer
WHILE NowPtr <> NullPtr
IF LinkList[NowPtr].Data = Item THEN
RETURN NowPtr // found: return its index
ELSE
NowPtr <- LinkList[NowPtr].NextPtr // advance
ENDIF
ENDWHILE
RETURN NullPtr // not found
ENDFUNCTION- A traversal is always: start at the head pointer → loop while the current pointer is not null → process the node → set current to its next pointer.
- Forget the final
NowPtr ← Nextand you get an infinite loop.
- Start at NowPtr=0; compare data; if no match advance to NextPtr; repeat.
- The loop only ends when NowPtr reaches the null pointer.
28.5 Traversing & Searching
28.6 Inserting & Deleting Nodes
- The whole point of a linked list is that you insert and delete by re-routing pointers, never by shifting data.
- This is the highest-value skill on the page and a frequent 5–8 mark question.
Insert (take a node from the free list):
To insert, you grab the first free node (at FreePointer), advance the free pointer, write the data, then re-link so the new node sits in the right place. Two pointer changes: the previous node points to the new node, and the new node points to what came next. No data is moved.
PROCEDURE InsertAfter(PrevPtr, NewData)
IF FreePointer = -1 THEN
OUTPUT "No free space"
ELSE
NewPtr <- FreePointer // take a free node
FreePointer <- LinkList[FreePointer].NextPtr // advance free list
LinkList[NewPtr].Data <- NewData
LinkList[NewPtr].NextPtr <- LinkList[PrevPtr].NextPtr
LinkList[PrevPtr].NextPtr <- NewPtr // re-link
ENDIF
ENDPROCEDUREDelete (return the node to the free list):
Delete by pointing the previous node past the deleted one. The freed node is then linked back into the free list.
- Find B at index 1; previous is index 0. Bypass: LinkList[0].NextPtr ← LinkList[1].NextPtr. Return node 1 to free list.
- Deleting the head is the special case: there is no “previous” node.
- Insert: take a node from the free list → advance free pointer → store data → re-link (previous.next → new, new.next → old next).
- Delete: find the node (tracking previous) → point previous.next past it (or move start if it's head) → return the freed node to the free list.
28.6 Inserting & Deleting Nodes
28.7 Linked List vs Array
- “Give one advantage and one disadvantage of a linked list compared with an array” is a guaranteed short-answer earner.
- Know the trade-offs cold.
| Linked list | Array |
|---|---|
| Grows/shrinks at run time (dynamic) | Usually fixed size |
| Insert/delete = change a few pointers | Insert/delete = shuffle many elements |
| No direct (random) access — must traverse | Direct access by index (fast) |
| Extra memory for each pointer | No pointer overhead |
- Advantage: dynamic size / cheap insert & delete by pointer change.
- Disadvantage: no direct access (must traverse) / extra memory for pointers.
- These are the standard answers examiners look for.
28.8 Full Exam-Style Question
- A waiting list at moshikur.com is held as a linked list using a 1D array of records
LinkList. - Each record has fields
DataandNextPtr. StartPointerindexes the first node,FreePointerindexes the first free node, and the null pointer is-1.
(a) [3] Describe three features of a linked list ADT. (AO1)
(b) [4] Complete the function SearchList(Item) that returns the index of the node containing Item, or -1 if it is not present. (AO2)
FUNCTION SearchList(Item) RETURNS INTEGER
NowPtr <- ....................
WHILE NowPtr <> ....................
IF LinkList[NowPtr].Data = Item THEN
RETURN ....................
ENDIF
NowPtr <- ....................
ENDWHILE
RETURN -1
ENDFUNCTION(c) [5] Write the procedure DeleteNode(Item) that removes the node containing Item by re-routing pointers and returns the freed node to the free list. Assume the node is not the head. (AO3)
(d) [2] State one advantage and one disadvantage of this linked list compared with storing the waiting list in a simple array. (AO1)
- (a) 3 from: node = data + pointer; start pointer; last node null; manipulate pointers not data; traverse in sequence; free list.
- (b) NowPtr ← StartPointer; WHILE NowPtr <> -1; RETURN NowPtr; NowPtr ← LinkList[NowPtr].NextPtr.
- (c) traverse keeping previous until Data = Item; handle not found; bypass (PrevPtr.NextPtr ← ThisPtr.NextPtr); return to free list (ThisPtr.NextPtr ← FreePointer); FreePointer ← ThisPtr.
- (d) advantage: dynamic / cheap insert+delete; disadvantage: no direct access / extra memory for pointers.
✓ Key Points Summary
28.9 Practice Tasks
Fifteen exam-style tasks. Click Hint for bullet-point guidance, then Help to reveal a worked Python/pseudocode solution.
Question Bank
Answer all questions, then press Submit Quiz to see your score.
Question 1Multiple Choice
A linked list is a sequence of items where each item (node) holds:
Question 2Multiple Choice
What marks the end of a linked list?
Question 3True / False
In a linked list, items are added/removed by manipulating pointers, not by moving data.
Question 4Multiple Choice
What is a free list?
Question 5Multiple Choice
In the two-array model, what are the two arrays?
Question 6Multiple Choice
In SearchList, what is the loop condition?
Question 7Multiple Choice
How do you advance in SearchList?
Question 8Multiple Choice
When inserting, where do you take the new node from?
Question 9True / False
Deleting the head is a special case because there is no previous node.
Question 10Multiple Choice
After deleting a node, where does the freed node go?
Question 11Multiple Choice
One advantage of a linked list over an array is:
Question 12Multiple Choice
One disadvantage of a linked list vs an array is:
Answer all 12 questions to enable submission.