Hey guys,
I'm looking for best algorithms to compare two Circular Linked List?
For example :
List 1 = 1-3-5
List 2 = 3-5-1
I want to compare "same" or "not same" but need fast algorithm.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Hey guys,
I'm looking for best algorithms to compare two Circular Linked List?
For example :
List 1 = 1-3-5
List 2 = 3-5-1
I want to compare "same" or "not same" but need fast algorithm.
Hi! Here I shared a little effort. Check it if this helps you.
Code:
public class LinkedList {
Object contents;
LinkedList next = null;
public boolean equals(Object item) {
return (this == item) || ((item instanceof LinkedList) && this.equals((LinkedList)item));
}
public boolean equals(LinkedList item) {
return myUtil.equals(this.contents, item.contents) && myUtil.equals(this.next, item.next);
}
}
public class myUtil{
public static boolean equals(Object x, Object y) {
return (x == y) || (x != null && x.equals(y));
}
}
main(){
LinkedList myList = new LinkedList();
myList.next = new LinkedList();
LinkedList head = myList.next;
myList.next = head;
}[COLOR="Silver"]
Last edited by roboticseducation; December 21st, 2017 at 01:21 AM. Reason: repeted text
The codes look a little different. Is the main() written like C++ programming? I understood some of the codes. Supposed to comparing List 1 and List 2. All this looks like it will be doing is comparing the outcomes of List 1 and List 2, by adding negative numbers. I must be missing the part where the items will be compared, for List 1 and List 2. Ok. I guess the List 1 and List 2 are not declared as Instance variables of a class. Then comparing the two object classes from the results. I hope I'm making some sense.