Linked-list operations and applications
Integrate insertion, deletion, traversal and search, including ordered insertion and exam-style edge cases.
Operations matter more than memorizing a diagram
- construct linked-list search, insertion, deletion and traversal in context;
- describe ordered insertion without relying on a later sort;
- handle empty/head/middle/end cases explicitly;
- evaluate whether linked-list trade-offs fit the application compared with an array/list structure.
Rapid Recall Deck
Attempt each card first. If a term is new, flip and study it, then answer it again without looking. Mark only secure recall as “Know it”.
Traverse without disturbing head
class PlayerNode:
def __init__(self, name, score):
self.name = name
self.score = score
self.next = None
class Leaderboard:
def __init__(self):
self.head = NoneConstruct search
Construct find_player(name) to return the first matching node, or None when absent.
def find_player(self, name):
current = self.head
while current is not None:
if current.name == name:
return current
current = current.next
return NoneDescribe the steps before translating them to code
new_node has already been created.Describe ordered insertion — no code yet
- If the new score should appear before the current head, point the new node to the old head and make it the new head.
- Otherwise traverse while the next node exists and should remain ahead of the new score.
- Set
new_node.nextto the predecessor’s oldnext. - Redirect the predecessor’s
nexttonew_node. If the predecessor was the tail, the saved next value isNone, so the new node becomes the tail.
Ordered insertion preserves structure and order
def add_in_order(self, new_node):
if self.head is None or new_node.score > self.head.score:
new_node.next = self.head
self.head = new_node
return
current = self.head
while current.next is not None and current.next.score >= new_node.score:
current = current.next
new_node.next = current.next
current.next = new_nodeTrace insertion
Start with scores 90 → 70 → 40. Trace insertion of score 75. Which node becomes current when the loop stops, and what is the final order?
current stops at the 90 node because the next score 70 is not greater than or equal to 75. The new node is inserted between 90 and 70, giving 90 → 75 → 70 → 40.
Explain pointer safety
Why does the method assign new_node.next = current.next before current.next = new_node?
The new node must first preserve the reference to the remainder of the list. Redirecting current.next first would remove the predecessor’s direct reference to that remainder.
Use the predecessor to bypass the removed node
Construct deletion by name
Construct remove_player(name). Return True if a node is deleted, otherwise False. Handle deletion of the head correctly.
def remove_player(self, name):
if self.head is None:
return False
if self.head.name == name:
self.head = self.head.next
return True
current = self.head
while current.next is not None:
if current.next.name == name:
current.next = current.next.next
return True
current = current.next
return FalseThe specimen expects both code and algorithm descriptions
Teacher-created Paper 2 mini-set
1. A sorted singly linked list stores 12, 18, 31, 44. Describe the reference changes needed to insert 25.
2. Construct a traversal that prints every stored score.
3. Evaluate maintaining a scoreboard as a linked list in sorted order when new entries are frequent but the program rarely accesses a specific numeric index.
- Traverse to 18, set the new node’s next to 31, then redirect 18’s next to the new node.
current = self.head while current is not None: print(current.score) current = current.next- A strong evaluation recognizes the insertion advantage once position is found and natural maintained order, balanced against O(n) traversal/search and extra reference memory; the usage makes linked lists plausible because indexed access is unimportant.
Robust code survives the boundaries
Exit edge cases
List four linked-list edge cases you would test for add_in_order() or remove_player(), and state what correct behaviour should look like.
Examples: empty list; new node belongs at head; new node belongs at tail; one-node list; deleting the head; deleting a middle/tail node; target absent; equal scores under the chosen ordering rule. Correct behaviour must preserve all remaining links and the ordering/preconditions.
Finish the learning cycle
IA — main task
Continue Criterion D implementation/testing and ensure your test evidence includes at least one boundary/edge case.
Paper 2 — short practice
Redo ordered insertion and deletion from memory. Then explain why the official specimen can ask for code in one part and a no-code algorithm description in another.