{"record":{"id":"44a430866d3aabfe","repo":"TheAlgorithms/Python","slug":"out-of-bounds","errorCode":null,"errorMessage":"Out of bounds","messagePattern":"Out of bounds","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"data_structures/linked_list/__init__.py","lineNumber":72,"sourceCode":"        Traceback (most recent call last):\n            ...\n        ValueError: Out of bounds\n        >>> linked_list.add(5, 4)\n        >>> print(linked_list)\n        3 --> 2 --> 4 --> 1 --> 5\n        \"\"\"\n        if position < 0:\n            raise ValueError(\"Position must be non-negative\")\n\n        if position == 0 or self.head is None:\n            new_node = Node(item, self.head)\n            self.head = new_node\n        else:\n            current = self.head\n            for _ in range(position - 1):\n                current = current.next\n                if current is None:\n                    raise ValueError(\"Out of bounds\")\n            new_node = Node(item, current.next)\n            current.next = new_node\n        self.size += 1\n\n    def remove(self) -> Any:\n        # Switched 'self.is_empty()' to 'self.head is None'\n        # because mypy was considering the possibility that 'self.head'\n        # can be None in below else part and giving error\n        if self.head is None:\n            return None\n        else:\n            item = self.head.item\n            self.head = self.head.next\n            self.size -= 1\n            return item\n\n    def is_empty(self) -> bool:\n        return self.head is None","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/data_structures/linked_list/__init__.py#L54-L90","documentation":"Raised by LinkedList.add(item, position) when the traversal `for _ in range(position - 1)` walks current to None before reaching the target slot — i.e. position exceeds the list length (strictly: position > size, since position == size appends). The node chain ended, so there is no node to attach after; ValueError('Out of bounds') fires. Note position == 0 and empty-list cases are handled earlier by the head-insert branch.","triggerScenarios":"add(5, 7) on a 4-element list (the doctest example); using position == size + 1 or more; computing position from a stale size after removals shrank the list.","commonSituations":"Insert-at-index code ported from lists where bounds drift; concurrent modification (list shrank between size check and add); off-by-one in append logic (use size, not size + 1).","solutions":["Clamp to append: `pos = min(pos, linked_list.size)` before calling","Re-check size immediately before the insert if the list may have changed","For pure append, call add(item, linked_list.size)"],"exampleFix":"# before\nlinked_list.add(5, 7)  # list has 4 nodes -> ValueError\n\n# after\nlinked_list.add(5, min(7, linked_list.size))  # appends","handlingStrategy":"validation","validationCode":"position = min(position, linked_list.size)\nlinked_list.add(item, position)","typeGuard":null,"tryCatchPattern":"try:\n    linked_list.add(item, position)\nexcept ValueError as e:\n    if 'Out of bounds' not in str(e):\n        raise\n    linked_list.add(item, linked_list.size)  # append instead","preventionTips":["Valid window is 0..size inclusive — clamp with min(pos, size) before calling","Recompute linked_list.size right before inserting; don't cache it","Off-by-one: appending needs size, not size + 1"],"tags":["linked-list","out-of-bounds","insert","index"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}