Returns true if the string represented - by the object it is on the same string is represented by the object str. Otherwise false is returned. This system must write the recursion without using any loops.

So I did it and it looks like this:

 public boolean equals ( StringList str ) 
    {
       if ((str._head == null) && (_head == null) )
       return  true;
       else 

        if( str._head.getData()==_head.getData() && str._head.getValue() == _head.getValue())
            {
            _head = _head.getNext();
            str._head = str._head.getNext();

               return  equals( str);  
           }
      return false;

    }

The problem is that I lose the head of the list and the entire list that as I progress I run over it .

Edited 3 Years Ago by YaLeon

example of Linked List
if I have string like :"aabb" so linked list will be " (head)a,2=> b,2=> null
if I have string like "abba" so linked list will be (head)a,1=>b,2=>a,1=> null

The quick and dirty way to fix it would probably be creating a separate instance variable to help in the recursion.

Edit: Bad approach, don't use it. See later posts.

Edited 3 Years Ago by mvmalderen

The problem is if you will do that every time recursevile it will be changed.

The problem is if you will do that every time recursevile it will be changed.

Yes, that's one of the reasons why I said "quick and dirty".
In your current version it is happening too though.

But I'm wondering, can't you create a private helper method that works on the node level?

Perhaps something like the following?

private boolean recursiveEquals(Node a, Node b) {
    // Make this your recursive method.
}

public boolean equals(StringList list) {
    return recursiveEquals(this._head, list._head);
}

Edited 3 Years Ago by mvmalderen

yeahh overloading will help here i'm pretty sure
i just tried to do that with out it.

thanks.

This question has already been answered. Start a new discussion instead.