Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

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

Aperta
#15,576 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@mohitjoshi-hey ci sta già lavorando.

Dal 14/10/2026.

  • #1 di @Aarya1777 — chiusa senza merge
  • #15577 di @mohitjoshi-hey — aperta
  • #15578 di @xingzihai — aperta
  • #15587 di @Aarya1777 — chiusa senza merge
  • #15588 di @Aarya1777 — aperta
  • #15589 di @krishna2707 — aperta
  • #15607 di @iamshresthraj — aperta

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
15/100
Tipo di issue
Refactoring
Chiarezza
Specificata chiaramente
Stato di attività
Ferma
Stack tecnologico
python
Ambito
backend

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

enhancement
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

Floyd’s cycle-finding algorithm

Lingua principale
Python
Stelle
225k
Fork
51.2k
Merge medio
1g 8h
PR unite (30g)
40

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di TheAlgorithms/Python

Tutte le issue di TheAlgorithms/Python

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.