Number of steps that a gambler has to play before the game ends A gambler plays a game, with probability$p$, he gets one coin and with probability $q$ he looses one coin. What is the number of games he has to play before he reaches 0 coin or n coins if he had k coins initially. 
Let $D_k$ be the number of steps before he reaches $0$ or $n$ dollars. Then $D_k=p(1+D_{k+1}) + q(1+ D_{k-1})$
 A: As mentioned in the comments, we assume $q=1-p$, also $D_0=D_n=0$.  Your recurrence for the expected number of steps is
$$pD_{k+1}-D_k+(1-p)D_{k-1}=-1.$$
First note that the system is inconsistent when $p=0,1$.  We can safely assume $0<p<1$ from now on.
We deal with the case $p\ne 1/2$ first.  Let $E_k=D_k+\frac{k}{2p-1}$.  So
$$pE_{k+1}-E_k+(1-p)E_{k-1}=0.$$The characteristic polynomial of this recurrence is 
$$px^2-x+(1-p)=p(x-1)\left(x-\frac{1-p}{p}\right).$$
That is
$$E_k=A+B\left(\frac{1-p}{p}\right)^k$$
so
$$D_k=A-\frac{k}{2p-1}+B\left(\frac{1-p}{p}\right)^k.$$
As $D_0=D_n=0$ we have $B=-A$ and
$$0=A-\frac{n}{2p-1}-A\left(\frac{1-p}{p}\right)^n.$$
Hence
$$A=\frac{n}{(2p-1)\left(1-\left(\frac{1-p}{p}\right)^n\right)}$$
so
$$D_k=\frac{n}{2p-1}\left(\frac{1-\left(\frac{1-p}{p}\right)^k}{1-\left(\frac{1-p}{p}\right)^n}\right)-\frac{k}{2p-1}.$$
For $p=1/2$, we can see that
$$D_{k+1}-2D_k+D_{k-1}=-2.$$
Let $E_k=D_k+k^2$.  We have
$$E_{k+1}-2E_k+E_{k-1}=0.$$
The characteristic polynomial of the recurrence above is
$$x^2-2x+1=(x-1)^2,$$
which has $1$ as a root of order $2$.  Hence $E_k=A+Bk$ for some $A,B$ and so $$D_k=A+Bk-k^2.$$  Because $D_0=D_n=0$ we get $A=0$ and $B=n$.  That is
$$D_k=nk-k^2=k(n-k).$$
