Go icon indicating copy to clipboard operation
Go copied to clipboard

fix: correct position validation and boundary handling in DelByPos

Open ahyalfan opened this issue 5 months ago • 4 comments

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 pos is 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:

ahyalfan avatar Sep 08 '25 06:09 ahyalfan

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.

github-actions[bot] avatar Oct 09 '25 00:10 github-actions[bot]

Still working on this, please keep it open.

ahyalfan avatar Oct 09 '25 06:10 ahyalfan

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.

github-actions[bot] avatar Nov 10 '25 00:11 github-actions[bot]

Still working on this, please keep it open.

ahyalfan avatar Nov 15 '25 07:11 ahyalfan