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 4558956
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:43:15+00:00 2026-05-21T17:43:15+00:00

I’m going to preface this with the fact that I am not completely knowledgeable

  • 0

I’m going to preface this with the fact that I am not completely knowledgeable on Big O Notation, so maybe my thinking about this is off.

I was randomly browsing SO when I came upon a question about detecting infinite loops in Linked Lists. This pointed me to the algorithm here known as the Turtle and Rabbit.

class Node {
  Node next;
}

boolean isInInfiniteLoop(Node node) {
  if (node == null) {
    return false;
  }
  Node turtle = node; // slower moving node
  Node rabbit = node.next; // faster moving node
  while (rabbit != null) {
    if (rabbit.equals(turtle)) {
      // the faster moving node has caught up with the slower moving node
      return true;
    } else if (rabbit.next == null) {
      // reached the end of list
      return false;
    } else {
      turtle = turtle.next;
      rabbit = rabbit.next.next;
    }
  }
  // rabbit reached the end
  return false;
}

In the article he mentions that it is O(N). From what I understand O(N) means that the speed of the algorithm grows linearly in relation to how many elements are in the list.

However, unless I am looking at things wrong, the rabbit variable is always skipping 1 node (so it’s “faster”) which means that it has the potential to skip over the turtle node, thus having the potential of looping around the infinite loop 1 or more times before it becomes the same node as the turtle variable, which would mean the worst-case scenario isn’t O(N).

Am I missing something? I guess an optimization might be to check if rabbit.Next.Equals(turtle) as well but as none of the comments point this out so I am wondering if I am missing something.

  • 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-05-21T17:43:16+00:00Added an answer on May 21, 2026 at 5:43 pm

    The rabbit will never jump over the turtle, because the difference between the speed is 1.

    The rabbit goes in the loop first, then the turtle. As soon as the turtle goes in the loop, the difference of the rabbit and the turtle is decided, and you can consider the rabbit is behind the turtle. Then is difference is simply decreased by 1 for each step.

    So the total steps will not exceed N, and thus it is O(n).

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

Sidebar

Related Questions

No related questions found

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.