remove the 0 at the end of the linked list

I had ...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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.