Skip to content

linked_list.delete_ith: out-of-bounds check and tail-deletion condition are incorrect #879

Closed
@EvanTsai370

Description

@EvanTsai370

In the delete_ith method of the LinkedList<T> implementation, there are two logic errors:

Bounds check allows index == length

if self.length < index {
    panic!("Index out of bounds");
}

This only panics when index > length, but permits index == length. The valid index range is 0..length, so any index >= length should panic.

Tail-deletion condition is off by one

if self.length == index {
    return self.delete_tail();
}

To delete the last element, the correct condition is index == length - 1, not index == length.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions