Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8256249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:47:57+00:00 2026-06-08T01:47:57+00:00

How to find the beginning node of a loop in a given linked list

  • 0

How to find the beginning node of a loop in a given linked list ? Let’s call this the cycle point

So far, I’ve understand the following (using slow/fast pointer):

  1. Assume list has a non-looped part of size k
  2. slow moves k steps
  3. fast moves 2k steps
  4. fast is (2k – k)= k steps ahead of slow
  5. slow is at the beginning of loop; also known as Cycle point
  6. fast is (LOOP_LENGTH - k) steps behind from Cycle point or slow pointer at this point
  7. for each 1 step slow moves, fast moves 2 steps and gains on slow by 1 step.
  8. Thus, it would take fast (LOOP_LENGTH - k) steps to meet slow and collide
  9. This is the step I don’t understand:
    At this collision point, both nodes will be k steps from the front of the loop.
  10. Once the collision point is found, move one pointer to the head of list.
  11. Now move both pointers at the speed of 1 step / turn till the collide. The node at which they both meet is the beginning of the the loop and hence the Cycle point

Can someone please explain me step 9 and after that ?

Thanks

EDIT:

One thing I’d like to point out is, once inside the loop, fast will never overtake slow pointer. They will collide. Here’s why: slow is at i and fast is assuming at i-1. when they move, slow=> i+1 and fast will be at i+1 too, hence collision. OR, slow is at i and fast is at i-2. next move, slow-> i+1; fast: i. next move, slow-> i+2, fast: i+2 and hence collision again. so fast will never be able to overtake slow, only collide once inside the loop!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T01:48:00+00:00Added an answer on June 8, 2026 at 1:48 am

    Your 6. is wrong, the fast pointer is still k steps away from the slow pointer which is at the cycle point at that time; but better use ahead or behind instead of away. Plus, k may be smaller, bigger, or equal to the loop_length.

    So, the fast pointer is k steps ahead of a slow one when that’s reached the loop point which is, at your supposition, k steps after the start. Now, measuring on a loop, the fast pointer is k % loop_length steps ahead of the loop point. Right? If k = some_n * loop_length + r, the fast pointer is r steps ahead of the loop point, which is to say, r := k % loop_length steps ahead.

    But that means that the slow pointer is loop_length - r steps ahead of the fast one, along the loop. This is a loop after all. So after loop_length - r additional steps the fast pointer will catch on to the slow one. For each step the slow pointer moves away, the fast moves closer in by two steps.

    So we don’t know k, we don’t know loop_length or r, we only know m = k + loop_length - r = some_n * loop_length + r + loop_length - r = (some_n+1) * loop_length. The total number of steps m until the two pointers’ meeting point, is a multiple of the loop length.

    So now we start over, with a new pointer at the start and the slow where it met the fast, m steps ahead of the new. We move the new and the slow at equal speed, by 1 step at each time, and at the cycle point they shall meet – because when the new pointer has reached the cycle point, the second is still m steps ahead, which is to say, m % loop_length == 0 steps ahead along the loop. That way we find out what k is (we count our steps all the time), and the cycle point.

    And we find loop_length by going along the loop one more time, until the two meet one more time.

    • see also: http://en.wikipedia.org/wiki/Cycle_detection#Algorithms
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need do find a cycle beginning and ending at given point. It is
find whether there is a loop in a linked list. Do you have other
I was practicing single linked list in c++ (practicing how to find the beginning
Our site has this call at the beginning to find the page's name (e.g.
I have a problem about deleting the last node of my linked list. I
I want to retain the beginning of the linked list in the code below.
This function find only first node in treeview, that contains SearchText . private TreeNode
I am looking to find anything that matches this pattern, the beginning word will
I have a Node structure as below,this Node has stringdata and list of nodes
i find a lot of questions about this problem but i did not solve...

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.