{"record":{"id":"b10750403d77df17","repo":"TheAlgorithms/Python","slug":"the-linked-list-is-empty","errorCode":null,"errorMessage":"The linked list is empty.","messagePattern":"The linked list is empty\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"data_structures/linked_list/rotate_to_the_right.py","lineNumber":99,"sourceCode":"    >>> rotate_to_the_right(None, places=1)\n    Traceback (most recent call last):\n        ...\n    ValueError: The linked list is empty.\n    >>> head = insert_node(None, 1)\n    >>> rotate_to_the_right(head, places=1) == head\n    True\n    >>> head = insert_node(None, 1)\n    >>> head = insert_node(head, 2)\n    >>> head = insert_node(head, 3)\n    >>> head = insert_node(head, 4)\n    >>> head = insert_node(head, 5)\n    >>> new_head = rotate_to_the_right(head, places=2)\n    >>> print_linked_list(new_head)\n    4->5->1->2->3\n    \"\"\"\n    # Check if the list is empty or has only one element\n    if not head:\n        raise ValueError(\"The linked list is empty.\")\n\n    if head.next_node is None:\n        return head\n\n    # Calculate the length of the linked list\n    length = 1\n    temp_node = head\n    while temp_node.next_node is not None:\n        length += 1\n        temp_node = temp_node.next_node\n\n    # Adjust the value of places to avoid places longer than the list.\n    places %= length\n\n    if places == 0:\n        return head  # As no rotation is needed.\n\n    # Find the new head position after rotation.","sourceCodeStart":81,"sourceCodeEnd":117,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/data_structures/linked_list/rotate_to_the_right.py#L81-L117","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Guard with 'if head is None: return None' before rotating.","Ensure the list-building path (insert_node chain) always produces at least one node, or short-circuit empty batches.","Catch ValueError if an empty rotation should be a no-op rather than an error."],"exampleFix":"# before\nnew_head = rotate_to_the_right(head, places=2)  # ValueError when head is None\n# after\nnew_head = head if head is None else rotate_to_the_right(head, places=2)","handlingStrategy":"validation","validationCode":"new_head = rotate_to_the_right(head, places=k) if head is not None else None","typeGuard":"def has_nodes(head) -> bool:\n    return head is not None","tryCatchPattern":"try:\n    new_head = rotate_to_the_right(head, places=k)\nexcept ValueError:\n    new_head = head  # empty list — nothing to rotate","preventionTips":["Short-circuit empty batches before rotating.","Single-node lists are safe; only None triggers the error.","places > length is fine — the function reduces it modulo the length."],"tags":["linked-list","value-error","rotate","empty-collection"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}