File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes how to identify loop in a linked list Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "how to identify loop in a linked list" Watch "how to identify loop in a linked list" New topic
Author

how to identify loop in a linked list

Shashank Rudra
Ranch Hand

Joined: Mar 26, 2009
Posts: 131
Hi Guys

We are given a linked list, that might have some loops. few of them can be seen as under


1 ---> 2 ---> 3 ---> 4
|                   ^
|                    |
|                    |
-----> 5 ------>

So here is a loop - 1 2 3 5.

How to find such loops? Does java linked list also have multiple pointers like in C++.


Programmer Analyst || J2EE web development/design
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16681
    
  19

The detecting of loops in a linked list, is a classic algorithm problem. There is actually a solution that can do it in O(n), which admittedly, if asked during an interview, I couldn't give the answer -- but I am sure it can be googled.

As for the Java collections implementations, I wouldn't worry about those. As long as you don't use threads on a non-synchronized collection, your chances of creating a loop is zero.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12907
    
    3

java.util.LinkedList is implemented as a doubly linked list, i.e. each node in the list has two pointers - one to the next and one to the previous element.

I don't understand the arrows in your picture. What exactly do the two arrows that come from element 1 mean with regard to LinkedList? Also, I don't see a loop in your picture. There's no way you can follow the arrows and come back to the element you started at. Sure, there are two paths from 1 to 3, but not a loop.

Are you really talking about linked lists, or is this really about finding loops in a graph?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Shashank Rudra
Ranch Hand

Joined: Mar 26, 2009
Posts: 131
Thanks Henry for your comments.

Here is a link that can be followed to get an overview of the same in terms of algorithm(logic).
http://ostermiller.org/find_loop_singly_linked_list.html

Some ways will not work in languages like Java where you don't have access to the memory address.

Some people might say that will get you a memory address. But I don't buy that, and think that is a hashcode basically which is returned by toString() method for each class unless they override it to some meaningful implementation. (please comment on this if not right)

A link related to java perspective is here
http://eddii.wordpress.com/2006/11/15/detecting-infinite-loop/


Any better link or comments guys.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3011
    
    9
Sam Giraldo wrote:... and think that is a hashcode basically which is returned by toString() method for each class unless they override it to some meaningful implementation. (please comment on this if not right)


Correct. From the javadoc for Object.toString()

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object.


Joanne
Shashank Rudra
Ranch Hand

Joined: Mar 26, 2009
Posts: 131
thanks Joanne for clearing this up.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: how to identify loop in a linked list
 
Similar Threads
parent child relation ship
How to Modify a MimeMessage
What data type ie collection or logic to be used for data?
Problem with abstraction
JPA MappingException: Repeated column in mapping for entity