B4.1.2 · Fundamentals of ADTs
Evaluating linked lists
A linked list trades an array's fast lookup for fast insertion and deletion, by giving up contiguous storage in favour of a chain of pointers.
Explainer
Think of a treasure hunt where each clue tells you where the next one is hidden. You can’t jump straight to clue 5; you have to follow the chain from clue 1, one step at a time. That’s a linked list: no shortcuts, just pointers leading to the next thing.
A linked list is a chain of nodes. Each node holds a value and a pointer to the next node in the chain. The list itself only needs to remember one thing: the head, a pointer to the first node. Everything else is found by following pointers, one node at a time.
This is the opposite trade-off to an array. An array stores every element in one contiguous block, so jumping straight to element 50 is instant. A linked list scatters its nodes wherever there's free memory, so reaching the 50th node means following 50 pointers, one after another, from the head.
Real example: a class register, seat by seat. The seating plan is fixed at the start of term, and you can jump straight to seat 27 without counting through the rest.
Advantage
- O(1) access by index (jump straight to any position, no need to follow anything first)
Disadvantage
- Fixed size (the size is set when created, growing past it means building a whole new, bigger array)
- O(n) insertion or deletion (every element after the gap has to shift along to close or open the space)
Real example: the treasure hunt above. Nothing is reserved in advance, each clue only exists once the previous one leads to it, and the hunt can grow by any number of clues without disturbing the ones already placed.
Advantage
- Dynamic size (grows and shrinks freely, no space reserved upfront the way an array reserves its full size immediately)
- O(1) insertion or deletion (once you're already at the right node, it's a single pointer reassignment, nothing shifts)
Disadvantage
- O(n) access, sequential only (no index to jump to; reaching node 50 means following 50 pointers from the head, one at a time, so a binary search is impossible on a linked list, only a linear search works)
- Extra memory per node (every node stores a pointer as well as its value, which adds up, and can risk running out of available memory at large scale)
Worked example
A chain of four nodes
Each node below stores a value and a pointer to the next node. The last node's pointer is None, which is how code recognises the end of the list.
Click a node to see what it points to.
Amir.next points to Beth. That's the only way to reach Beth from the head: there's no shortcut, no index, just this one pointer.
What is node.next? It's not a value. It's a reference to another node object, the next one in the chain, or None if this is the last node. Writing node.next.value gets you the next node's value; node.next alone just gets you the next node itself.
# Node holds one value and a pointer to the next node class Node: def __init__(self, value): self.value = value self.next = None # Building Amir -> Beth -> Chen by hand, one pointer at a time head = Node("Amir") head.next = Node("Beth") head.next.next = Node("Chen") # Traversal: start at head, follow .next until it's None current = head while current is not None: print(current.value) current = current.next
// Node holds one value and a pointer to the next node class Node { String value; Node next; Node(String value) { this.value = value; this.next = null; } } // Building Amir -> Beth -> Chen by hand, one pointer at a time Node head = new Node("Amir"); head.next = new Node("Beth"); head.next.next = new Node("Chen"); // Traversal: start at head, follow .next until it's null Node current = head; while (current != null) { System.out.println(current.value); current = current.next; }
The guide's current generator only produces Python. Java is shown here as a reference for schools teaching that track; both build the chain the same way, one pointer assignment at a time.
Check your understanding
Beth needs to be removed from the chain Amir → Beth → Chen → Dara. Which single change does that?
Common mistake
B4.1.2 / B4.1.3RecurringMarkers reported candidates losing marks on insertion and deletion questions for describing the outcome in general terms without naming which pointer actually changes and what it now points to. A linear, value-based description ("search through the list to find Beth, then remove her") reads like an array explanation, not a linked-list one.
The stronger responses were consistently the ones written in terms of pointers: which node's next field is being read, checked, or reassigned, at every step.
What a weak answer looks like: "Find Beth in the list and delete her." No pointer named, no node named as the one being changed.
What it should say: "Traverse from head until the current node's next is Beth. Set that node's next to Beth's next, skipping Beth entirely." Names the pointer, names what it's set to.
Revision questions
Past-paper style. Write an answer, then reveal the marking points and mark yourself.
[1]State what a node's next pointer contains when that node is the last one in the list.
[3]Evaluate the use of a linked list, rather than an array, for a playlist that songs are frequently added to and removed from.
Marking points
评分要点
- Advantage: adding or removing a song only rewires a couple of pointers, whereas an array would have to shift every song after the change point.
- Advantage: the list uses exactly as much memory as there are songs, growing and shrinking as the playlist changes.
- Disadvantage: jumping to the nth song means following n pointers from the head, where an array reaches it directly by index.
- Disadvantage: each node needs extra memory for its pointer.
- Evaluation: for a playlist edited often but usually played in order, the linked list is a good fit; if random access by track number were common, an array would be better.
- 优点:添加或移除一首歌只需重连几个指针,而数组必须移动改动点之后的每一首歌。
- 优点:链表使用的内存恰好与歌曲数量相当,随播放列表的变化而增长和收缩。
- 缺点:跳到第 n 首歌意味着从头跟随 n 个指针,而数组通过索引直接到达。
- 缺点:每个节点需要额外内存存放它的指针。
- 评价:对于经常编辑但通常按顺序播放的播放列表,链表是好的选择;如果按曲目编号的随机访问很常见,数组会更好。
[4]Describe, in terms of pointers, the steps needed to insert a new node between Beth and Chen in the chain above.
Marking points
评分要点
- Create the new node holding its value.
- Set the new node's next pointer to the node currently after Beth (that is, Chen).
- Set Beth's next pointer to the new node.
- Now Beth points to the new node, and the new node points to Chen, so the chain is: Beth, new node, Chen.
- 创建持有其值的新节点。
- 把新节点的 next 指针设为当前在 Beth 之后的节点(即 Chen)。
- 把 Beth 的 next 指针设为新节点。
- 现在 Beth 指向新节点,新节点指向 Chen,所以链是:Beth、新节点、Chen。
Flashcards
Active recall for this statement's key terms.
Keep this statement's practice with you
Free account: unlock every revision question and flashcard for B4.1.2, and every other IB CS 2027 statement.