class List;
List *deletezeroendlist(List* L);
class List
{
public:
int digit;
List* nextDigit;
public:
List():digit(0), nextDigit(NULL){}
List(int d, List *next):digit(d), nextDigit(next){}
int getDigit(){
return digit;
}
List* getNextDigit(){
return nextDigit;
}
};
List *deletezeroendlist(List* L)
{
if (L->nextDigit== NULL)
{
if (L->digit!=0) {return L;}
else {delete L; return L;}
}
elsereturn deletezeroendlist(L->nextDigit);
}
I have tried many different ways but it is still not the answer
help me perform the function List *deletezeroendlist(List* L)
EX
L=12345600 -> L=123456
or L=01203000 -> L=01203
Removing a node from the list requires that you update the pointer in the previous node.
Removing suffix requires that you start from end and repeat that as long as necessary.
Do you have to recurse? Iteration could be simpler.
You have to consider case, when
0 != L->nextDigit
0 == L->nextDigit->nextDigit
IF 0 == L->nextDigit->digit
THEN
remove L->nextDigit and ensure that L is consistent
ELSE
there are no trailing nulls
Thing is, if there was a trailing null, then you have to find the second to last again.