Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Better wording in tortoise_and_hare.md#1520

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
kostca merged 4 commits intocp-algorithms:mainfromarjunUpatel:patch-4
Sep 22, 2025
Merged
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletionssrc/others/tortoise_and_hare.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -15,8 +15,8 @@ Here we need to find out the point **C**, i.e the starting point of the cycle.

## Proposed algorithm
The algorithm is called **Floyd’s Cycle Algorithm or Tortoise And Hare algorithm**.
In order to figure out the starting point of the cycle, we need to figure outof the thecycle even exists or not.
So, it involved two steps:
In order to figure out the starting point of the cycle, we need to figure outif acycle even exists.
This involves two steps:
1. Figure out the presence of the cycle.
2. Find out the starting point of the cycle.

Expand All@@ -26,14 +26,14 @@ So, it involved two steps:
3. $slow$ will move one step at a time.
4. $fast$ will move two steps at a time. (twice as speed as $slow$ pointer).
5. Check if at any point they point to the same node before any one(or both) reach null.
6. If they point toany same node at any point of their journey, itwould indicatethatthe cycle indeed exists in the linked list.
7. If we get null, itwould indicate that the linked list has no cycle.
6. If they point tothe same node at any point of their journey, itindicatesthata cycle indeed exists in the linked list.
7. If we get null, itindicates that the linked list has no cycle.

<div style="text-align: center;">
<img src="tortoise_hare_cycle_found.png" alt=""Found cycle"">
</div>

Now, that we have figured outthat there is a cycle present in the linked list, for the next step we need to find out the starting point of cycle, i.e., **C**.
Now, that we have figured outif there is a cycle present in the linked list, for the next step we need to find out the starting point of cycle, i.e., **C**.
### Step 2: Starting point of the cycle
1. Reset the $slow$ pointer to the **head** of the linked list.
2. Move both pointers one step at a time.
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp