Skip to main content
SingaporeComputer ScienceSyllabus dot point

How does a linked list store a sequence using pointers, and how does it differ from an array?

Describe a linked list as nodes joined by pointers, implement insertion and deletion, and contrast it with an array

A focused answer to the H2 Computing outcome on linked lists. Nodes and pointers, traversal, inserting and deleting nodes by relinking, and the trade-offs against arrays for access and modification.

Generated by Claude Opus 4.88 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this dot point is asking
  2. The answer
  3. Examples in context
  4. Try this

What this dot point is asking

SEAB wants you to describe a linked list as nodes joined by pointers, implement insertion and deletion, and contrast it with an array. The central idea is that a linked list trades the array's instant indexing for cheap insertion and deletion: because nodes are connected by pointers rather than stored contiguously, you change links instead of shifting elements.

The answer

Nodes and pointers

A linked list is a sequence of nodes. Each node holds a value and a pointer (reference) to the next node. A head pointer marks the start, and the last node's pointer is null to mark the end:

class Node:
    def __init__(self, value):
        self.value = value
        self.next = None

# head -> [A|*] -> [B|*] -> [C|null]

Nodes need not sit together in memory; the pointers thread them into order.

Traversal

To visit every node, start at the head and follow next until you reach null:

def traverse(head):
    current = head
    while current is not None:
        print(current.value)
        current = current.next

There is no index arithmetic, so reaching position ii takes ii steps: access is O(n)O(n).

Insertion and deletion by relinking

The payoff is cheap structural change once you have the right node. To insert node X after node A:

  1. Set X.next = A.next (point X at whatever A pointed to).
  2. Set A.next = X.

The order matters: link X first, or the rest of the list is lost. To delete the node after A, set A.next = A.next.next, bypassing it. No elements shift, so given the position these are O(1)O(1).

Linked list versus array

Operation Array Linked list
Access position ii O(1)O(1) O(n)O(n)
Insert/delete at front O(n)O(n) O(1)O(1)
Memory layout contiguous scattered + pointers
Extra memory none a pointer per node

Arrays give instant random access; linked lists give cheap insertion and deletion and grow without reallocation, at the cost of pointer storage and slow indexing.

Examples in context

Example 1. A music playlist. A playlist where songs are frequently inserted and removed mid-list is a natural linked list: dropping a track between two others just reroutes two pointers, with no need to shift the rest of the queue as an array would require.

Example 2. Building stacks and queues. A stack or queue with no fixed capacity is often built on a linked list, because pushing or enqueuing is an O(1)O(1) relink at one end and the structure grows one node at a time without reallocating a whole array. The linked list provides exactly the cheap end-operations these structures need.

Try this

Q1. State the time complexity of accessing the 5th node of a linked list and explain why. [2 marks]

  • Cue. O(n)O(n) - you must follow next pointers from the head, counting steps, with no index arithmetic.

Q2. Describe the pointer changes to delete the node after node A in a singly linked list. [2 marks]

  • Cue. Set A.next = A.next.next, bypassing the node; it is then unreferenced and reclaimed. No elements shift.

Q3. Give one advantage of a linked list over an array. [1 mark]

  • Cue. O(1)O(1) insertion or deletion at the front (or at a known node) by relinking, and it grows without reallocating.

Exam-style practice questions

Practice questions written in the style of SEAB exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.

Original6 marksA singly linked list holds nodes A -> B -> C, where the head points to A and C points to null. Describe the pointer changes needed to (a) insert a new node X between A and B, and (b) delete node B from the original list.
Show worked answer →

Each node stores a value and a pointer to the next node.

(a) To insert X between A and B:

  1. Create node X.
  2. Set X.next to point to B (the node A currently points to).
  3. Set A.next to point to X.

Order matters: set X.next before changing A.next, or the reference to B is lost. The list becomes A -> X -> B -> C.

(b) To delete B from A -> B -> C:

  1. Set A.next to point to C (the node B points to, B.next).
  2. B is now unreferenced and can be reclaimed.

The list becomes A -> C. No elements are shifted; only pointers are rerouted.

Markers reward creating and linking X in the safe order for insertion, and rerouting A.next to B.next for deletion, with the point that only pointers change.

Original5 marksCompare a singly linked list with an array for (a) accessing the element at a given position, and (b) inserting an element at the front, giving the time complexity of each and a one-line reason.
Show worked answer →

(a) Accessing position ii: an array is O(1)O(1) - the address is computed directly by index. A linked list is O(n)O(n) - you must follow next pointers from the head, counting ii steps, because nodes are scattered in memory with no index arithmetic.

(b) Inserting at the front: a linked list is O(1)O(1) - create a node, point it at the old head, and update the head. An array is O(n)O(n) - every existing element must shift right by one to make room.

So arrays win on random access, linked lists win on front (and general) insertion and deletion once the position is known.

Markers reward the two complexities for each operation with correct reasons: index arithmetic versus pointer following, and pointer relinking versus shifting.

Related dot points