Hi all, I have a doubt regarding finding a loop in a linked list.I searched on internet and found 2 different ways of doing the same. But my doubt is that if a linked list has a loop, then wouldn't it be a circular linked list? If yes, then the loop can be found by testing each node's address field with NULL. Am I right? Awaiting a response.

Recommended Answers

All 2 Replies

But my doubt is that if a linked list has a loop, then wouldn't it be a circular linked list?

Not necessarily. This is a circular linked list:

a -> b -> c -> d -+
^                 |
|                 |
+-----------------+

This is a list with a loop, but it's not a circular linked list because the last node doesn't link back to the first node, it links to a node somewhere in the middle:

a -> b -> c -> d -+
          ^       |
          |       |
          +-------+

If yes, then the loop can be found by testing each node's address field with NULL.

How do you know when to stop looking? That's the point of the question.

I understood your point. Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.