1

Detecting cycles in a single linked list is a well known problem. I know that this question has been asked a zillion times all over the internet. The reason why I am asking it again is I thought of a solution which I did not encounter at other places. (I admit I haven't searched that deeply either).

My solution is:

Given a linked list and pointer to some node, break the link between node and node->next(); Then start at node->next() and traverse till either you hit an end (which means there was no loop) or till you reach at node which means there was a loop.

Is there anything wrong/good about above solution ?

Note: Do join the link back once you are done.

asked Nov 26, 2011 at 15:26
3
  • Most of the solutions suggest that the two "runners" with different speeds as the best solution. Happens to be O(n), Isnt my solution O(n) too ? Commented Nov 26, 2011 at 15:27
  • I don't see any reason for breaking the link, just treat node as a sentinel. A problem: if node is a, then your algorithm will not work on a->b->c->d->e->c. Your algorithm will only detect loops where the last node points to the first node. Commented Nov 26, 2011 at 15:34
  • This does not work, if the loop isn't global. Example: a -> b -> c -> b -> c -> b -> c -> b -> ... never returns to a. Commented Nov 26, 2011 at 15:39

3 Answers 3

2

That will work to detect complete cycles (i.e., cycles with a period of the whole list), e.g.:

A -> B -> C -> D -> A

But what if we have a cycle somewhere else in the list?

e.g.,

A -> B -> C -> D -> E -> C

I can't see that your algorithm will detect the cycle in this case.

Keep in mind that to detect the first case, we need not even break the link. We could just traverse the list and keep comparing the next link for each node with the head element to see if we'd started back at the start yet (or hit the end).

answered Nov 26, 2011 at 15:31
0

I guess the most trivial approach (not necessarily the best, but one that everybody should know how to implement in Java in a few lines of code) is to build a Hash Set of the nodes, start adding them until you find one that you already saw before. Takes extra memory though.

If you can mark nodes, start marking them until you find one you marked before (the hash map is essentially an external marker).

And check the usual graph theory books...

answered Nov 26, 2011 at 15:38
0

You are not allowed to break a link, even if you join it back at the end. What if other programs read the list at the same time ?

The algorithm must not damage the list while working on it.

answered May 5, 2013 at 12:39

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.