fix: correct position validation and boundary handling in DelByPos
Description of Change
Problem
The original DelByPos function had incorrect position validation and boundary checks, causing errors when deleting nodes at valid positions, especially at the start and end of the list.
Specifically, the conditions used pos-1 inconsistently, leading to off-by-one errors and incorrect calls to deletion methods.
Solution
- Updated position validation to correctly check if
posis within[1, ll.length]. - Fixed boundary checks to correctly call DelAtEnd()
whenpos == ll.length`. - Adjusted the switch cases accordingly to reflect 1-based indexing for positions.
- Added test cases to cover successful deletions at various positions and to ensure invalid positions return false.
Testing
- Added new tests for
DelByPos()covering success and failure cases. - All tests pass.
Checklist
- [x] Added description of change (https://github.com/TheAlgorithms/Go/blob/master/CONTRIBUTING.md#New-File-Name-guidelines)
- [x] Added tests and example, test must pass
- [x] Relevant documentation/comments is changed or added
- [x] PR title follows semantic commit guidelines
- [x] Search previous suggestions before making a new one, as yours may be a duplicate.
- [x] I acknowledge that all my contributions will be made under the project's license.
Notes:
This PR is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.
Still working on this, please keep it open.
This PR is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.
Still working on this, please keep it open.