2

So i have a linked list that I want to be able to remove the first occurrence of a number,

I'm trying to use recursion but sadly all I end up doing is being able to delete the head of the list and

public List remove(int num){
   if(value == num) {
       return next.remove(value);
   }else{
       next = next.remove(value);
       return this;
   }
}

I know i need to return the new list, but how exactly do i either get rid of the node that I'm trying to avoid or is there a way to work around it, so it continues to the next nod.

Edit. Update on the actual code.

class List{
  int value;  //value at this node 
  List next;  //reference to next object in list
  public List(int value, List next){
      this.value = value;
      this.next  = next;
  }
}

I have three different classes, one for the empty list at the end of this, and a class declaring this method, and the actual list.

  public static List makeSample() {
        EmptyList e = new EmptyList();
        List l1 = new List(5, e);
        List l2 = new List(4, l1);
        List l3 = new List(3, l2);
        List l4 = new List(3, l3);
        List l5 = new List(2, l4);
        List l6 = new List(1, l5);
        return l6;
    }
7
  • Do you want to create a copy of the list without the element, or change the list inplace? Commented Nov 18, 2012 at 2:23
  • 1
    Create a copy of the list without the element. Commented Nov 18, 2012 at 2:31
  • 1
    Can you clarify what List you are using? Is it a java.util.List (docs.oracle.com/javase/7/docs/api/java/util/List.html) or something of your own? Where are the variables value and next coming from? You say you want to return a copy of the list, but in fact you are just returning the same reference this. Commented Nov 18, 2012 at 2:31
  • I tried to answer this question but I realized I don't know enough about how your implementation is structured exactly. Commented Nov 18, 2012 at 2:34
  • @user1832483 First, I believe you are using your own List structure and not the java.util.List. So can you please post code complete class definition for the List class. Commented Nov 18, 2012 at 2:36

2 Answers 2

2

Try this

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class List {

    private int value;
    private List next;

    public static final List EMPTY = new List(-1, null) {
        public List remove(int n) { return this; };
        public String toString() { return ""; };
    };

    public List(int value, List next) {
        this.value = value;
        this.next = next;
    }

    public List remove(int n) {
        if (value == n) return next;
        return new List(value,next.remove(n));
    }   

    public String toString() {
        return value + "," + next.toString();
    }

    public static class Examples {

        @Test
        public void shouldRemoveElement() {
            List l = new List(1, new List(2, new List(2, new List(3, EMPTY))));
            assertEquals("1,2,2,3,",l.toString());
            assertEquals("2,2,3,",l.remove(1).toString());
            assertEquals("1,2,3,",l.remove(2).toString());
            assertEquals("1,2,2,",l.remove(3).toString());
            assertEquals("1,2,2,3,",l.toString());
        }

    }

}
Sign up to request clarification or add additional context in comments.

5 Comments

I tryed implementing your code, maybe im not following right but why exactly are you using next.value, don't you have put .get before value to be able to compare them?
In Java, fields are class private, that is an instance can access the private fields of another instance of the same class. (Did the code compile for you?)
no the code wouldn't compile saying it cannot find the symbol variable value which doesn't make sense to me because its declared in the constructor.
So how exactly is the code working? I havent learned the null ? null code yet so that's throwing me off.
null works the same way as your EmptyList. Actually using EmptyList is smarter, it is called a nullobject. Google for Nullobject Pattern to learn more.
0

Keep two lists.

List nonDuplicates;

List allElements;

for (Node node : allElements){
    if (!nonDuplicates.contains(node)){
        nonDuplicates.add(node);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.