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;
}
java.util.List(docs.oracle.com/javase/7/docs/api/java/util/List.html) or something of your own? Where are the variablesvalueandnextcoming from? You say you want to return a copy of the list, but in fact you are just returning the same referencethis.Liststructure and not thejava.util.List. So can you please post code complete class definition for theListclass.