A linked list joins nodes through references rather than consecutive indexes. Each node can hold a value and a pointer to the next node. Trace the references from the head until the next reference is None.
Watch out: Losing the head reference or replacing a link too early can make later nodes unreachable.
Study this example, identify inputs and outputs, then trace the key variables. Run and adapt it in Python 3.
class Node:
def __init__(self, value, next_node=None):
self.value, self.next = value, next_node
class LinkedList:
def __init__(self): self.head = None
def prepend(self, value): self.head = Node(value, self.head)
def to_list(self):
result, current = [], self.head
while current is not None:
result.append(current.value)
current = current.next
return result
linked = LinkedList()
for item in (3, 2, 1): linked.prepend(item)
2 · Independent programming challenge
Build a singly linked list and traverse nodes to collect their values. Download the starter, design your solution, and add tests before revealing the answer.
Stretch task: investigate time/space complexity or data validation, then write an evaluation of your approach.
Show reference solution after attempting the task
class Node:
def __init__(self, value, next_node=None):
self.value, self.next = value, next_node
class LinkedList:
def __init__(self): self.head = None
def prepend(self, value): self.head = Node(value, self.head)
def to_list(self):
result, current = [], self.head
while current is not None:
result.append(current.value)
current = current.next
return result
linked = LinkedList()
for item in (3, 2, 1): linked.prepend(item)
assert linked.to_list() == [1, 2, 3]
print(linked.to_list())
3 · Debug and explain
Change one boundary condition in the starter or reference program. Predict which test fails, then repair it and document why.
Concept check: What does the head pointer store?
Reveal explanation
A reference to the first node, or None when the linked list is empty.
4 · Assess your learning
Describe the algorithm or design in your own words; include a normal test, a boundary test and an invalid input. Compare your answers with the reference solution and record one improvement.
Draw nodes as values plus links. When inserting a node, consider link-update order carefully so that the rest of the list remains reachable. Contrast linked traversal with direct indexing into an array.
Quick check: Why can inserting into a linked list require link changes rather than shifting many stored elements?
Check: Each node points to the next; insertion updates references instead of physically moving array elements.