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.

Results 1 to 3 of 3

Thread: I'm looking for best algorithms to compare two Circular Linked List?

  1. #1
    Junior Member
    Join Date
    Dec 2017
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I'm looking for best algorithms to compare two Circular Linked List?

    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.

  2. #2

    Default Re: I'm looking for best algorithms to compare two Circular Linked List?

    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

  3. #3
    Member
    Join Date
    Dec 2013
    Location
    Honolulu
    Posts
    83
    Thanks
    1
    Thanked 4 Times in 2 Posts

    Default Re: I'm looking for best algorithms to compare two Circular Linked List?

    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.

Similar Threads

  1. Looking for help with a circular linked list
    By artAlias in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 21st, 2013, 05:40 PM
  2. Need a little help removing from a circular linked list
    By bankston13 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 20th, 2012, 02:50 AM
  3. help with circular linked list
    By Seans0007 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 12th, 2012, 02:37 PM
  4. Help with a doubly linked circular list
    By TeamRival in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 3rd, 2011, 10:59 PM
  5. circular linked list
    By student123xyz in forum Collections and Generics
    Replies: 4
    Last Post: August 19th, 2009, 10:40 AM