TheAlgorithms/Python · error · ValueError

The linked list is empty.

Error message

The linked list is empty.

What it means

rotate_to_the_right raises ValueError('The linked list is empty.') when head is None; rotating nothing is treated as a caller error. A single-node list (head.next_node is None) is legal and returned unchanged, and 'places' longer than the list is normalized via modulo internally.

Source

Thrown at data_structures/linked_list/rotate_to_the_right.py:99

    >>> rotate_to_the_right(None, places=1)
    Traceback (most recent call last):
        ...
    ValueError: The linked list is empty.
    >>> head = insert_node(None, 1)
    >>> rotate_to_the_right(head, places=1) == head
    True
    >>> head = insert_node(None, 1)
    >>> head = insert_node(head, 2)
    >>> head = insert_node(head, 3)
    >>> head = insert_node(head, 4)
    >>> head = insert_node(head, 5)
    >>> new_head = rotate_to_the_right(head, places=2)
    >>> print_linked_list(new_head)
    4->5->1->2->3
    """
    # Check if the list is empty or has only one element
    if not head:
        raise ValueError("The linked list is empty.")

    if head.next_node is None:
        return head

    # Calculate the length of the linked list
    length = 1
    temp_node = head
    while temp_node.next_node is not None:
        length += 1
        temp_node = temp_node.next_node

    # Adjust the value of places to avoid places longer than the list.
    places %= length

    if places == 0:
        return head  # As no rotation is needed.

    # Find the new head position after rotation.

View on GitHub (pinned to f5988cc097)

Solutions

  1. Guard with 'if head is None: return None' before rotating.
  2. Ensure the list-building path (insert_node chain) always produces at least one node, or short-circuit empty batches.
  3. Catch ValueError if an empty rotation should be a no-op rather than an error.

Example fix

# before
new_head = rotate_to_the_right(head, places=2)  # ValueError when head is None
# after
new_head = head if head is None else rotate_to_the_right(head, places=2)
Defensive patterns

Strategy: validation

Validate before calling

new_head = rotate_to_the_right(head, places=k) if head is not None else None

Type guard

def has_nodes(head) -> bool:
    return head is not None

Try / catch

try:
    new_head = rotate_to_the_right(head, places=k)
except ValueError:
    new_head = head  # empty list — nothing to rotate

Prevention

When it happens

Trigger: rotate_to_the_right(None, k) — the doctest itself builds the list via insert_node(None, 1), so passing an unbuilt head is the trap; passing a head that a prior operation set to None.

Common situations: Rotation applied in a loop over per-user or per-batch lists where some batches are empty; refactors that replaced list construction with a function returning None on empty input; forgetting insert_node's first call takes None.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/b10750403d77df17. Report an issue: GitHub.