Linked Lists
Follow a chain of nodes and see how links let a list grow without shifting a row of slots.
linked list, node, link, head, traversal, insertionFollow a chain of notes
Imagine each note tells you where to find the next note. You start at the first, then follow each instruction until there is no next note.
A linked list uses the same idea. A node holds a value and a link to another node. The head is the link to the first node. In our singly linked list, each node points only to the next one.
head
|
v
[2 | next] --> [5 | next] --> [9 | None]
The arrows show connections, not a promise that nodes are next to one another
in memory. None means there is no next node.
Why use it and where it helps
When we already have a link to the front, adding a new first node only changes a few links; it does not shift every existing item. Linked nodes can also be used to build other structures, including a stack or a queue.
That does not make linked lists universally faster. A Python list is an
array-like sequence, not a linked list: it offers fast indexed access and is
often a simpler choice. Nodes need extra space for links and can have poorer
memory locality.
Add a new first node
Start with head -> [5] -> [9] -> None. We want to put 2 at the front.
| Step | What changes | Chain |
|---|---|---|
| 1 | Create a node holding 2 | [2] |
| 2 | Point its next link at the old head | [2] -> [5] -> [9] -> None |
| 3 | Make the new node the head | head -> [2] -> [5] -> [9] -> None |
If the list was empty, the old head was None; the new node simply points to
None. If we lose the old head before linking it, the rest of the chain can
become unreachable from the new head.
See the reasoning
Follow the example one step at a time
Add a new head without losing the existing chain. This is a prepared teaching demonstration, not live code execution.
Start
head → [5] → [9] → None
The head points to the first node.
Python illustration
class Node:
def __init__(self, value):
self.value = value
self.next = None
def push_front(head, value):
node = Node(value)
node.next = head
return node
def values_from(head):
values = []
current = head
while current is not None:
values.append(current.value)
current = current.next
return values
head = None
for number in [9, 5, 2]:
head = push_front(head, number)
print(values_from(head))
print(values_from(None))
Prepared output — not live execution:
[2, 5, 9]
[]
Each call returns the new head, so the caller assigns it back to head.
values_from follows links and creates a separate Python list for display.
Time, space, and common mistakes
For n nodes, adding at the front is O(1) when the head is available.
Reaching an index or searching by value is O(n) in the worst case because we
may follow every link. A full traversal takes O(n) time. Our values_from
copy also uses O(n) extra space; traversal without a copy can use O(1).
Adding or removing after a known node can change links in O(1), but finding that node may first cost O(n). Our example assumes an acyclic chain; a cycle could make the traversal loop forever. Other linked-list variants and their operations are planned later.
Reflect on the concept
Four fixed questions to reinforce the reasoning. Answers are not saved or scored.
