|
-
September 26th, 2012, 01:11 AM
#12
Re: How to find whether a linked list is a circular linked list
 Originally Posted by jblackwood3
but would this also work?
That's what I suggested and it will work. Sooner or later when you traverse a list you either reach the end or if it's circular or has a loop you come back to where you were before and that can be detected by storing all visited node pointers.
I wrote hashmap but I meant hashset. You really don't need the counters. You only need to know whether a node pointer is already present or not and for that a hashset is fine.
That said the other approach with two racing pointers at different speeds probably is better, although a little harder to get your head around. Here's a summary. The hashset solution is categorized as "correct but space inefficient",
http://ostermiller.org/find_loop_sin...nked_list.html
Last edited by nuzzle; September 26th, 2012 at 01:17 AM.
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
|