Skip to content

Optimize linked-list loop detection - #15577

Open
mohitjoshi-hey wants to merge 1 commit into
TheAlgorithms:masterfrom
mohitjoshi-hey:optimize-has-loop-constant-space
Open

mohitjoshi-hey wants to merge 1 commit into
TheAlgorithms:masterfrom
mohitjoshi-hey:optimize-has-loop-constant-space

Conversation

@mohitjoshi-hey

Copy link
Copy Markdown

Use Floyd's cycle detection algorithm to reduce has_loop auxiliary space from O(n) to O(1).

Describe your change

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

The Node.has_loop property in data_structures/linked_list/has_loop.py previously detected cycles by traversing the list with a visited-node set, which required O(n) auxiliary memory.

This change replaces that approach with Floyd's tortoise-and-hare cycle detection algorithm. The behavior remains unchanged while reducing auxiliary space usage to O(1).

The implementation compares node identities rather than node data values, so it correctly handles different nodes containing equal data.

Validation

The following checks passed:

Screenshot 2026-10-15 022218
.venv\Scripts\python.exe -m doctest -v data_structures/linked_list/has_loop.py
7 passed
Test passed.

.venv\Scripts\python.exe -m py_compile data_structures/linked_list/has_loop.py
Passed

git diff --check
Passed

Memory measurements using tracemalloc showed the following results:

List size Before peak memory After peak memory
1,000 43,912 bytes 0 bytes
10,000 697,672 bytes 0 bytes
50,000 2,794,888 bytes 0 bytes
100,000 6,924,712 bytes 0 bytes

The same result was observed for both acyclic lists and cyclic lists.

Checklist

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All function and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues, then the description above includes the issue number(s) with a closing keyword: "Fixes Optimize Node.has_loop to use constant auxiliary space instead of O(N) space #15576 ".

I have reviewed the contributing guidelines and the Code of Conduct.

Use Floyd's cycle detection algorithm to reduce has_loop auxiliary space from O(n) to O(1).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@algorithms-keeper algorithms-keeper Bot added awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files labels Oct 14, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Optimize Node.has_loop to use constant auxiliary space instead of O(N) space

1 participant