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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:38:03+00:00 2026-06-02T07:38:03+00:00

How can I detect that whether a singly linked-list has loop or not?? If

  • 0

How can I detect that whether a singly linked-list has loop or not??
If it has loop then how to find the point of origination of the loop i.e. the node from which the loop has started.

  • 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-02T07:38:06+00:00Added an answer on June 2, 2026 at 7:38 am

    You can detect it by simply running two pointers through the list, this process is known as the tortoise and hare algorithm after the fable of the same name:

    • First off, check if the list is empty (head is null). If so, no cycle exists, so stop now.
    • Otherwise, start the first pointer tortoise on the first node head, and the second pointer hare on the second node head.next.
    • Then loop continuously until hare is null (which may be already true in a one-element list), advancing tortoise by one and hare by two in each iteration. The hare is guaranteed to reach the end first (if there is an end) since it started ahead and runs faster.
    • If there is no end (i.e., if there is a cycle), they will eventually point to the same node and you can stop, knowing you have found a node somewhere within the cycle.

    Consider the following loop which starts at 3:

    head -> 1 -> 2 -> 3 -> 4 -> 5
                      ^         |
                      |         V
                      8 <- 7 <- 6
    

    Starting tortoise at 1 and hare at 2, they take on the following values:

    (tortoise,hare) = (1,2) (2,4) (3,6) (4,8) (5,4) (6,6)
    

    Because they become equal at (6,6), and since hare should always be beyond tortoise in a non-looping list, it means you’ve discovered a cycle.

    The pseudo-code will go something like this:

    def hasLoop (head):
      return false if head = null           # Empty list has no loop.
    
      tortoise = head                       # tortoise initially first element.
      hare = tortoise.next                  # Set hare to second element.
    
      while hare != null:                   # Go until hare reaches end.
        return false if hare.next = null    # Check enough left for hare move.
        hare = hare.next.next               # Move hare forward two.
    
        tortoise = tortoise.next            # Move tortoise forward one.
    
        return true if hare = tortoise      # Same means loop found.
      endwhile
    
      return false                          # Loop exit means no loop.
    enddef
    

    The time complexity for this algorithm is O(n) since the number of nodes visited (by tortoise and hare) is proportional to the number of nodes.


    Once you know a node within the loop, there’s also an O(n) guaranteed method to find the start of the loop.

    Let’s return to the original position after you’ve found an element somewhere in the loop but you’re not sure where the start of the loop is.

    head -> 1 -> 2 -> 3 -> 4 -> 5
                      ^         |
                      |         V
                      8 <- 7 <- 6
                                 \
                                  x (where hare and tortoise met).
    

    This is the process to follow:

    • Advance hare and set size to 1.
    • Then, as long as hare and tortoise are different, continue to advance hare, increasing size each time. This eventually gives the size of the cycle, six in this case.
    • At this point, if size is 1, that means you must already be at the start of the cycle (in a cycle of size one, there is only one possible node that can be in the cycle so it must be the first one). In this case, you simply return hare as the start, and skip the rest of the steps below.
    • Otherwise, set both hare and tortoise to the first element of the list and advance hare exactly size times (to the 7 in this case). This gives two pointers that are different by exactly the size of the cycle.
    • Then, as long as hare and tortoise are different, advance them both together (with the hare running at a more sedate pace, the same speed as the tortoise – I guess it’s tired from its first run). Since they will remain exactly size elements apart from each other at all times, tortoise will reach the start of the cycle at exactly the same time as hare returns to the start of the cycle.

    You can see that with the following walkthrough:

    size  tortoise  hare  comment
    ----  --------  ----  -------
       6         1     1  initial state
                       7  advance hare by six
                 2     8  1/7 different, so advance both together
                 3     3  2/8 different, so advance both together
                          3/3 same, so exit loop
    

    Hence 3 is the start point of the cycle and, since both those operations (the cycle detection and cycle start discovery) are O(n) and performed sequentially, the whole thing taken together is also O(n).


    If you want a more formal proof that this works, you can examine the following resources:

    • a question on our sister site;
    • the Wikipedia cycle detection page; or
    • “The Tortoise and the Hare Algorithm” by Peter Gammie, April 17, 2016.

    If you’re simply after support for the method (not formal proof), you can run the following Python 3 program which evaluates its workability for a large number of sizes (how many elements in the cycle) and lead-ins (elements before the cycle start).

    You’ll find it always finds a point where the two pointers meet:

    def nextp(p, ld, sz):
        if p == ld + sz:
            return ld
        return p + 1
    
    for size in range(1,1001):
        for lead in range(1001):
            p1 = 0
            p2 = 0
            while True:
                p1 = nextp(p1, lead, size)
                p2 = nextp(nextp(p2, lead, size), lead, size)
                if p1 == p2:
                    print("sz = %d, ld = %d, found = %d" % (size, lead, p1))
                    break
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I detect whether or not an Image View has a picture in
I've got an input box that allows UTF8 characters -- can I detect whether
I need to write a small program that can detect that it has been
Microsoft DxDiag can detect whether a system has Direct3D Acceleration. If the system has
You can detect whether a popup has been blocked in Chrome with the solutions
How can WCF client detect that server requires security certificate? The detection should happen
Is there a Regular Expression that can detect SQL in a string? Does anyone
I need a library that can detect objects in an image (uses edge detection).
Is there a way in which I can detect the URL that is calling
There are a lot of programs, Visual Studio for instance, that can detect when

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.