Make the recursive call that returns the next one, in this way you can assign it to the current recursive call if you have not deleted it. That is, when calling the following:
- If
null
returns null
to assign it to the previous one.
- If it is the one that you have to eliminate, assign the following one to a temporary one (since you are going to eliminate it), delete it, and make the recursive call with the following one.
- If you do not have to delete it, assign the following as the next recursive call to this one.
Or said in a more summarized way:
- If you do not delete the next one - > you assign it
- If you delete the next one - > assign the following one that you are not going to eliminate (or
null
if there are no more).
I leave you the recursive function code:
lista remover(int x, lista l) {
if (l == NULL) {
return l; // Devolvemos NULL
} else {
if (l->elem == x) {
lista tmp = l->sig; // Asignamos el siguiente a un temporal.
delete l; // Eliminamos el actual.
return remover(x, tmp); // Devolvemos lo que devuelva la llamada recursiva con el siguiente
}
else {
l->sig = remover(x, l->sig); // Asignamos lo que devuelva la llamada recursiva con el siguiente.
return l; // Devolvemos el actual;
}
}
}
I hope to have explained myself, but do not hesitate to comment on the answer and try to explain myself better.
How you comment @Xam You can use nullptr
instead of null
from C ++ 11.