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

  • Home
  • SEARCH
  • 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 8540179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:32:20+00:00 2026-06-11T11:32:20+00:00

There’s a really elegant solution to this problem here , but I don’t understand

  • 0

There’s a really elegant solution to this problem here, but I don’t understand the biggest part – why, after moving S back to the beginning of the list, are S and F the same distance from the loop start? He does some math to “prove” it, but it doesn’t quite make sense to me. Any help in understanding this better would be much appreciated. Thanks!

  • 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-11T11:32:21+00:00Added an answer on June 11, 2026 at 11:32 am

    To frame the problem, we assume that there is an n node loop starting m nodes past the start and that we are walking it with a slow pointer S (one node each step) and a fast pointer F (two nodes each step). For the sake of brevity, we assume that m < n, but it doesn’t matter so much (just do some modular arithmetic).

    The key is to realize that S and F will overlap at node n - m. The math done in the article is admittedly a little hard to follow, and doesn’t seem to generalize to odd n. Not sure this will be much easier, but I’ll try.

    Suppose that S starts at the beginning of the loop and F starts k nodes past the start of the loop and we start walking the loop. At time step x, S will be x nodes past the start of the loop and F will be 2x + k nodes past the start of the loop. Of course, F will not overtake S until F has crossed the start of the loop, at which point we can equivalently describe it as being (2x + k) - n = 2x - (n - k) nodes past the start.

    We now ask, “at what step x will S and F overlap?” That is simply when the position of S is equal to the position of F, so x = 2x - (n - k), or (with some simple algebra), x = n - k. Thus, both pointers will be n - k nodes past the start of the loop.

    Back to the original problem (both pointers start from the head of the list), by the time S hits the start (in m steps), F will have traveled 2m steps, and thus will be m nodes past the start of the loop (2m - m = m). Replace k with m above and we see that when we continue the nodes will overlap when pointer F (and S) are n - m nodes past the start of the loop. Thus, if we move S back to the beginning, both F and S will take m steps to get back to the beginning of the loop.

    Let me know if this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There have been multiple questions regarding this topic but I have never really settled
There are nice SO question and answers about this issue, but these options didn't
There was a similar question asked here three years ago, but I want to
There are plenty of questions and answers to this like this question but I
There is a column that exists in 2 tables. In table 1, this column
There's a Rails 3.2.3 web application which doesn't use any database. But in spite
For some reason, after submitting a string like this Jack’s Spindle from a text
There is this strange situation I'm fighting on. I have 3 pages, les call
There are a lot of questions about full-joining in mysql(5.1.36). Of course the solution
There was some progress made to the earlier problem. Now there is a new

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.