Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8113069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:42:35+00:00 2026-06-06T02:42:35+00:00

public class LinkedList { Object contents; LinkedList next = null; public boolean equals(Object item)

  • 0
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;
}

I think i have created a circular linkedlist here. So what i have done is to overwrite the equals method to ensure that circular references are handled:

For some reason the LinkedList.equals doesnt seem to return…is it because of my circular linkedlist, or am i missing some conditions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T02:42:37+00:00Added an answer on June 6, 2026 at 2:42 am

    The primary problem with this code is that your comparison will not terminate upon circular reference, and will loop forever if all contents fields are equal. It will always continue to the next comparison, and since the next item is always there (as it’s a circle) this will continue forever.

    myUtil.equals(this.contents, item.contents) && myUtil.equals(this.next, item.next);
    

    To solve this issue, the simplest method would be to add a boolean private ‘visited’ field to each List item. When you compare, set visited on each item after the comparison. If both are not visited and the same, then continue. If only one is visited, your lists are not identical. If both are visited, you’ve compared the reachable entirety of the list. Generally, having loops in your list are a bad idea, and there exist algorithms specifically to detect them. This can be a confusing topic. Here is a coverage of loop detection that may help you understand the issue further. Remember, if you use the visited field, you must unset all of them with another loop in your equals() to allow it to run again.

    On another note, you do not initialize the contents field of your list nodes for the test. This is okay here, since they are initialized to null, but generally it is good practice to explicitly initialize all your fields.

    Generally speaking, you also don’t need the equals(Object item) override. Try

    public boolean equals(LinkedList item){
      if (this == item){
         return true; // It's the same object
      }
    
      // Add some null checks here, I'm lazy
    
      if (this.visited && item.visited && this.contents.equals(item.contents){
         this.visited = false; //Unset
         item.visited = false;
         return true;
      }
      if (this.visited && !item.visited){
          this.visited = false;
          return false;
      }
      if (!this.visited && item.visited){
          item.visited = false;
          return false;
      }
      if (!this.visited && !item.visited && this.visited.contents.equals(item.contents){
          this.visited = true;
          item.visited = true;
          boolean ret = this.next.equals(item.next);
          this.visited = false;
          item.visited = false;
          return ret;
      }
    
      // Contents not equal
      return false;
    }
    

    This backtracks and unsets with some basic recursion. I obviously haven’t compiled this, but that’s the gist of it, I think (I hope there aren’t too many errors)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a recursive object, a linked list really: public class LinkedList { public
class ListNode { public object Data { get; private set; } public ListNode Next
I'm confused by the following code: import java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class
I have code that looks like this: public class Polynomial { List<Term> term =
I have this class: public abstract class Directory { protected int id; protected File
When I attempt to insert this 'food' object into my template class linked list
So basically I have this constructor for the class League : import java.util.*; public
class ZiggyTest{ public static void main(String[] args){ List<Integer> list = new LinkedList<Integer>(); list.add(4); list.add(5);
If I have a collection of objects: public class Party { LinkedList<Guy> partyList =
I have a class class PCB { public: struct { string type; **linklist list;**//refer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.