Understanding Linked Lists

An OCR A-Level Computer Science walkthrough: what a linked list is, and how to remove and add nodes by changing pointers

By Hashir Ali, , 3 min read

In this week's A-Level lesson we worked through an OCR exam question on linked lists. A coach company stores the cities on a tour in order: London, Oxford, Birmingham, Manchester. The tour changes, so Birmingham has to be removed and York added to the end. It's a 4-mark question, and every mark comes from describing a pointer change precisely.

Annotated OCR exam question from the lesson, showing Oxford's pointer redirected past Birmingham to Manchester, and York added after Manchester
From the lesson: redirecting Oxford's pointer around Birmingham, then linking York on the end.

What is a linked list?

A linked list is a dynamic data structure made of nodes. Each node holds two things:

  • data, such as the name of a city
  • a pointer giving the location of the next node

A separate head pointer stores where the list starts, and the last node's pointer is null to mark the end. Because the order comes from the pointers, not from where the nodes sit in memory, you can add and remove items without shuffling any data around.

The original tour. Manchester's pointer is null, so it is the last node.

For a 3-mark "describe" question, those three points are the answer: dynamic, data and pointer in each node, and the pointer giving the location of the next node.

Removing a node

To remove Birmingham:

  • Traverse from the head until you reach the node before Birmingham, which is Oxford.
  • Change Oxford's pointer to Birmingham's pointer, so it now points to Manchester.
Oxford now bypasses Birmingham. Birmingham's data is still in memory, but nothing points to it.

Notice that Birmingham isn't deleted or overwritten. It simply can't be reached any more, and its space can be returned to the free list (or tidied up by garbage collection) to be reused later.

Adding a node

To add York to the end of the tour:

  • Create a new node holding "York" in the next free space in memory.
  • Manchester stays where it is, but its pointer changes from null to point to York.
  • York's pointer is set to null, because it is now the last node.
The amended tour, with the new York node on the end.

Adding in the middle works the same way, but order matters: set the new node's pointer to the next node first, then point the previous node at the new one. Do it the other way round and you lose the only link to the rest of the list.

The mark scheme answer as a diagram

You can also answer this question by drawing on the diagram. Each (1) shows where a mark is awarded: Oxford's pointer going around Birmingham, Manchester's pointer to York, the new York node, and York's null pointer.

Mark scheme diagram: Oxford's pointer bypasses the greyed-out Birmingham node to reach Manchester, Manchester points to a new York node, and York's pointer is null, with one mark for each change
The OCR mark scheme's diagram answer, worth all 4 marks.

Where marks are lost

  • Forgetting to create the node. "Manchester points to York" only works if York exists. Say it is created in the next free space.
  • Forgetting the null. The new last node needs a null pointer, or the list has no end.
  • Moving data. Nothing shifts position. Only pointers change, which is the whole point of a linked list compared with an array.
  • Vague wording. "Birmingham is removed" scores nothing. Say whosepointer changes and what it now points to.

Key takeaways

  • Each node holds data and a pointer to the next node; the last pointer is null.
  • Remove a node by pointing the previous node at the one after it.
  • Add a node by creating it, linking it in, and giving the last node a null pointer.

Data structure questions like this turn up on every OCR A-Level paper. If you'd like help turning understanding into full marks, take a look at our exam preparation lessons.

Want this kind of help every week?

Start with a free 30-minute trial, no obligation. GCSE lessons are £55/hour and A-Level lessons are £60/hour — pay as you go, no contracts.

Book a free 30-min trial