|
-
October 13th, 2008, 04:47 PM
#6
Re: How to find whether a linked list is a circular linked list
 Originally Posted by ProgramThis
First off, if you have a dummy header pass the search method head->next, then you are doing exactly what I have recommended.
If you perform a linear scan for a fixed pointer on a 6 shaped circular list then the procedure will not terminate.
Specifically, with the circular list:
node_1 -> node_2 -> node_3 -> node_4 -> node_2
an attempt to follow the procedure given will fail:
traverse to the last object and see whether or not it has a reference to the "head" object. If it does, great, otherwise it should point to null.
The procedure doesn't detect that node_4 is the last - it has neither a reference to null or to node_1, the head.
The algorithm with the slow and fast will terminate in that case.
The OP asked to find whether the list is circular or not, not whether or not it had cycles.
Evidently we have been reading different references to glean our terminology; in the Lisp standard, any list with a cycle is circular.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|