One-Way Node Traversal¶
class Node:
def __init__(self, value, next_node=None):
self.value = value
self.next = next_node
head = Node("A", Node("B", Node("C")))
current = head
while current:
print(current.value)
current = current.next
A singly linked list moves in one direction, one node at a time¶
The linked-lists overview introduced nodes and `next` links. This notebook makes that idea concrete: each node has exactly one forward reference, so traversal is simple and predictable, but backward movement is not built in.
Key Idea¶
A singly linked list only moves in one direction: from the current node to the next node.
This is enough for sequential traversal, but not for moving backward efficiently.
Continuity from linked lists
The last notebook introduced nodes and links in general. Here the structure becomes more specific: every node only knows the next node, not the previous one.
Worked Example and Practice Lab¶
Expected output
A
BThe first example shows the basic structure: the head node points forward, and each step follows one next reference.
Expected output
['A', 'B', 'C']Singly Linked-List Operations¶
These smaller examples separate node creation, traversal, and insertion in a one-way list.
Create nodes¶
A singly linked-list node stores a value and one next reference.
Traverse forward¶
Move through the list by repeatedly following next. This is the fundamental singly linked-list pattern.
Insert at the front¶
To add a new first node, point it to the current head. Front insertion is simple because the new node only needs one forward link.
Append at the end¶
To append, walk to the last node and set its next reference.
Exercises¶
Explain why a singly linked list is called “singly.”
Describe what happens during forward traversal.
Compare inserting at the front with appending at the end.
Explain what extra information a doubly linked list would need to support backward movement.
Hint
Look closely at the Node class: each node stores exactly one forward connection.
Guided Practice¶
How many forward references does a singly linked-list node typically store?¶
What is the traversal output of the second example?¶
Key Takeaway¶
A singly linked list is simple because each node only needs to know the next node. That simplicity makes forward traversal natural, and it sets up the contrast with doubly linked lists, where nodes can move both ways.