TheAlgorithms/Python · error · Exception

Node not found

Error message

Node not found

What it means

DoublyLinkedListTwo.get_node raises a generic Exception('Node not found') after walking from head to the end without matching item. Every consumer of get_node inherits this, notably delete_value, which despite its walrus 'if (node := self.get_node(value)) is not None' check can never reach the None branch — get_node raises instead of returning None.

Source

Thrown at data_structures/linked_list/doubly_linked_list_two.py:139

    def insert_at_position(self, position: int, value: DataType) -> None:
        current_position = 1
        new_node = Node(value)
        node = self.head
        while node:
            if current_position == position:
                self.insert_before_node(node, new_node)
                return
            current_position += 1
            node = node.next
        self.set_tail(new_node)

    def get_node(self, item: DataType) -> Node:
        node = self.head
        while node:
            if node.data == item:
                return node
            node = node.next
        raise Exception("Node not found")

    def delete_value(self, value):
        if (node := self.get_node(value)) is not None:
            if node == self.head:
                self.head = self.head.next

            if node == self.tail:
                self.tail = self.tail.previous

            self.remove_node_pointers(node)

    @staticmethod
    def remove_node_pointers(node: Node) -> None:
        if node.next:
            node.next.previous = node.previous

        if node.previous:
            node.previous.next = node.next

View on GitHub (pinned to f5988cc097)

Solutions

  1. Wrap get_node/delete_value calls in try/except Exception and match on message 'Node not found' (generic Exception forces message matching).
  2. Pre-verify the value exists by traversing or maintaining a set of current values before calling delete_value.
  3. Subclass and override get_node to return None instead, which makes the existing 'is not None' check in delete_value work as designed.

Example fix

# before
linked.delete_value(maybe_absent)  # Exception: Node not found
# after
try:
    linked.delete_value(maybe_absent)
except Exception as e:
    if 'Node not found' not in str(e):
        raise
Defensive patterns

Strategy: try-catch

Validate before calling

# pre-walk to confirm existence
cur = dll.head
exists = False
while cur:
    if cur.data == value:
        exists = True
        break
    cur = cur.next
if exists:
    dll.delete_value(value)

Try / catch

try:
    dll.delete_value(value)
except Exception as e:
    if 'Node not found' not in str(e):
        raise

Prevention

When it happens

Trigger: delete_value(v) or get_node(v) where no node's data == v; searching with the wrong type (str vs int); searching after the node was already unlinked by a prior delete.

Common situations: Caller assumes delete_value is a silent no-op for missing values (the walrus suggests a None check that never triggers); batch removals where a value occurs zero times; deserialized data with type drift.

Related errors


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