Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

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
B

The 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

  1. Explain why a singly linked list is called “singly.”

  2. Describe what happens during forward traversal.

  3. Compare inserting at the front with appending at the end.

  4. 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?

TwoThat is more typical of a doubly linked list.
OneCorrect. A singly linked node points forward to one next node.
Zero alwaysOnly the final node usually has no next reference.
ThreeThat is not the typical design here.

What is the traversal output of the second example?

['C', 'B', 'A']The code starts at the head and moves forward.
['A', 'B', 'C']Correct. That is the forward traversal order.
['A', 'C']Node `B` is also visited.
[]The list is not empty.

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.