### remove the 0 at the end of the linked list

 ``12345678910111213141516171819202122232425262728293031323334353637383940`` ``````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;} } else return 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

remove the 0 at the end
thanks you very much!
Why/what do you return from that method?

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.
like this do not you? Help me...thanks you so much
 ``123456789101112131415`` ``````List *deletezeroendlist(List* L) { List * pTemp =new digitList; if (pTemp == NULL) { if (pTemp->digit!=0) return L; else delete pTemp;return L; } else {pTemp=L->nextDigit;return deletezeroendlist(pTemp);} }``````
Last edited on
That is worse.

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.
Topic archived. No new replies allowed.