Skip to content

Conversation

wilson20010327
Copy link
Contributor

Original function will face infinite loop when there is a mid-list loop in queue. Transform the is_circular function by shifting from a single pointer approach to employing slow and fast pointers. The single pointer method falls short in detecting loops that occur mid-list without additional storage. Utilizing slow and fast pointers overcomes this limitation, enabling the detection of mid-list loops. Although this approach introduces a slight increase in conditional checks, it remains a superior method for loop detection without the need for extra storage.

Transform the is_circular function by shifting from a single
pointer approach to employing slow and fast pointers. The
single pointer method falls short in detecting loops that
occur mid-list without additional storage. Utilizing slow
and fast pointers overcomes this limitation, enabling
the detection of mid-list loops. Although this approach
introduces a slight increase in conditional checks, it remains
a superior method for loop detection without the need for extra storage.
Copy link
Contributor

@jserv jserv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summarize your work in the subject of this pull request and associated git commit message. Instead of "Fix," use proper actions such as "Prevent ... from ..."

@wilson20010327 wilson20010327 changed the title Fix the is_circular function Prevent is_circular function from infinite loop Mar 28, 2024
To follow the coding style, remove the usless parentheses.
@jserv jserv merged commit 9bda828 into sysprog21:master Apr 1, 2024
@jserv
Copy link
Contributor

jserv commented Apr 1, 2024

Thank @wilson20010327 for contributing!

Huadesuwa pushed a commit to Huadesuwa/lab0-c that referenced this pull request Mar 21, 2025
This commit refines the is_circular() to use slow and fast pointers
instead of a single pointer iteration. The original approach struggled
to detect mid-list loops without extra storage. The new method
effectively identifies such loops, albeit with a minor increase in
conditional checks, but without requiring additional storage.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants