Let $M_{i}$ denote the mean number of games until the gambler either goes broke or reaches a fortune of $N$, given that he starts with $i = 0,1,\ldots,N$.
I have shown that $M_{0} = M_{N} = 0$ and $M_{i} = 1 + pM_{i+1} + qM_{i-1}$ for $i=1,\ldots,N-1$
I want to solve these equations to obtain $$M_{i} = i(N-i)$$if $p=1/2$ or $$M_{i} = \frac{i}{q-p} - \frac{N}{q-p}\cdot\frac{1 - (q/p)^{i}}{1- (q/p)^{N}}$$ if $p \neq 1/2$.
I've tried verifying by direct substitution, but I don't think I'm substituting correctly. How can I solve these difference equations to show the desired result?
The recurrence relation
$$pM_{i+1} - m_i +qM_{i-1} = -1 \qquad\qquad (1)$$
is a non-homogeneous, second-order, linear recurrence relation with constant coefficients ($p,-1,q$) and can be solved by finding its "characteristic equation" as described here or here or in most texts on discrete mathematics.
Initially, consider the homogeneous relation
$$pM_{i+1} - m_i +qM_{i-1} = 0. \qquad\qquad (2)$$
The characteristic equation is $pr^2 - r + q = 0$ giving roots: $\;r=1 \text{ or } q/p$.
$$\\$$
Case $p=q$:
We have a double root $r=1$ so the general solution to $(2)$ is
$$M_i = A + Bi\qquad\qquad (3)$$
The particular solution for $(1)$ would normally have form $M_i=C$ because RHS $(1)$ is constant, but in view of $(3)$ we instead try $M_i = Ci^2$ and substitute this into $(1)$ to give
$$-1 = pC(i+1)^2 - Ci^2 + qC(i-1)^2 = C \qquad\text{using $p=q=1/2$}.$$
So our particular solution is $M_i=-i^2$ and our general solution to $(1)$ is $M_i = A+Bi-i^2$.
$M_0 = 0 \implies 0=A.$
$M_N = 0 \implies 0 =BN-N^2\;$ so $\;B=N$.
So the solution to $(1)$ is
$$M_i = Ni-i^2.$$
$$\\$$
Case $p\neq q$:
We have distinct roots $r=1,\; q/p\;$ so the general solution to $(2)$ is
$$M_i = A + B\left(q/p\right)^i \qquad\qquad (4)$$
Here we try the particular solution $M_i = Ci$ and substitute this into $(1)$ to give
$$-1 = pC(i+1) - Ci + qC(i-1) = C(p-q).$$
Therefore, $\; C=1/(q-p)\;$ and our general solution to $(1)$ is $M_i = A + B\left(q/p\right)^i + \dfrac{i}{q-p}$.
$M_0 = 0 \implies 0=A + B\;$ so $\;A=-B.$
$M_N = 0 \implies 0 = -B + B\left(q/p\right)^N + \dfrac{N}{q-p}\;$ so $\;B=\dfrac{N}{q-p}\cdot \dfrac{1}{1-\left(q/p\right)^N}$.
So the solution to $(1)$ is
\begin{align} M_i &= - \dfrac{N}{q-p}\cdot \dfrac{1}{1-\left(q/p\right)^N} + \dfrac{N}{q-p}\cdot \dfrac{\left(q/p\right)^i}{1-\left(q/p\right)^N} + \dfrac{i}{q-p} \\ & \\ &= \dfrac{i}{q-p} - \dfrac{N}{q-p}\cdot \dfrac{1-\left(q/p\right)^i}{1-\left(q/p\right)^N}. \end{align}