Optimize Node.has_loop to use constant auxiliary space instead of O(N) space
Maintainer antworten meist innerhalb von 1 Tag
@mohitjoshi-hey arbeitet bereits daran.
Seit 14.10.2026.
Bewertung
- Schwierigkeit
- 2/5
- Geschätzter Aufwand
- 1-3 Stunden
- Anfängerfreundlichkeit
- 15/100
Rechercherichtung
The has_loop property in data_structures/linked_list/has_loop.py currently builds a set of visited nodes via the iterator. Linked PR #15577 is already open against this issue, so check that PR before starting, since the work is under way. The change would replace the set with Floyd's tortoise-and-hare pointers, and done means the listed cases pass: acyclic, self-loop, back-edge, tail loop, and duplicate data values.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
Feature description
Optimize has_loop to use constant auxiliary space
Description
The has_loop property in data_structures/linked_list/has_loop.py currently detects loops by traversing the linked list and storing every visited node in a set:
@property
def has_loop(self) -> bool:
try:
list(self)
return False
except ContainsLoopError:
return True
The iterator uses:
visited = set()
and adds each visited node to that set. As a result, detecting whether a linked list contains a loop requires O(n) additional memory.
Proposed change
Update only the has_loop property in:
data_structures/linked_list/has_loop.py
Use Floyd’s tortoise-and-hare cycle-detection algorithm:
- A slow pointer moves one node at a time.
- A fast pointer moves two nodes at a time.
- If the pointers meet, the linked list contains a loop.
- If the fast pointer reaches the end of the list, no loop exists.
Expected complexity
Time complexity: O(n)
Auxiliary space: O(1)
Required behavior
The updated has_loop property should correctly detect:
- an acyclic linked list;
- a self-loop;
- a loop back to an earlier node;
- a loop involving the final node;
- lists containing duplicate data values in different nodes.
Loop detection must compare node references rather than node data values.
Reference
- Vorherrschende Sprache
- Python
- Sterne
- 225k
- Forks
- 51.2k
- Ø Merge
- 1 T. 8 Std.
- Gemergte PRs (30 T.)
- 40
Entwicklungsumgebung
Startet den Dev-Container des Projekts im Browser, mit Ihrem eigenen GitHub-Konto.
- Kein Dockerfile und keine Docker-Compose-Datei
- Hat eine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus TheAlgorithms/Python
-
ciphers/base32.py and ciphers/base85.py encode Unicode code points instead of bytes: output diverges from the spec for non-ASCII, round-trips corrupt chars >= U+0400, arbitrary bytes crashEvtl. vergeben @Nate-br hat das vor 1 Tag übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 88/100
TheAlgorithms/Python#15636 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
ciphers/des_ecb.py: encrypt/decrypt silently corrupts any character above U+00FF (round-trip fails)Evtl. vergeben @mohammedanasnakhuda hat das vor 2 Tagen übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 88/100
TheAlgorithms/Python#15634 ·
Maintainer antworten meist innerhalb von 1 Tag
-
other/password.py: alternative_password_generator ignores the requested length when the required characters exceed itEvtl. vergeben @raaj-2222 hat das vor 2 Tagen übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 85/100
TheAlgorithms/Python#15633 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
ciphers/base58.py is a copy of base64_cipher.py — no Base58 implementation exists in the fileEvtl. vergeben @SakshiYeole hat das vor 2 Tagen übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 45/100
TheAlgorithms/Python#15637 ·
Maintainer antworten meist innerhalb von 1 Tag
-
hashes/adler32.py: checksum diverges from the Adler-32 spec for any non-ASCII input (sums Unicode code points instead of bytes)Evtl. vergeben @SwetaKumari7 hat das vor 3 Tagen übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 35/100
TheAlgorithms/Python#15635 ·
Maintainer antworten meist innerhalb von 1 Tag
Alle Issues in TheAlgorithms/Python
Ähnliche Issues
-
backlog
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 91/100
EchoTools/nevr-runtime#1108 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
btclib-org/btclib-wallet#587 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
Maintainer antworten meist innerhalb von 1 Tag
-
documentation
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
Judgment-Pack/judgment-pack-spec#275 ·
Maintainer antworten meist innerhalb von 1 Tag