Expected time for winning in biased Gambler's Ruin Consider the random walk $X_0, X_1, X_2, \ldots$ on state space $S=\{0,1,\ldots,n\}$ with absorbing states $A=\{0,n\}$, and with $P(i,i+1)=p$ and $P(i,i-1)=q$ for all $i \in S \setminus A$, where $p+q=1$ and $p,q>0$.
Let $T$ denote the number of steps until the walk is absorbed in either $0$ or $n$.
Let $\mathbb{E}_k(\cdot) := \mathbb\{\cdot | X_0 = k\}$ denote the expectation conditioned on starting in state $k \in S \setminus A$.
How to compute $\mathbb{E}_k(T|X_T = n)$?
 A: This is an appendix to the excellent answer of Did, giving an explicit formula for $u_k$ (whose derivation involves quite a lot of work).
$u_k$, that is $E_k(T:X_T=n)$, can be expressed as:$$
\frac{
    n \cdot (1 - r^k) \cdot (1 + r^n)
  - k \cdot (1 + r^k) \cdot (1 - r^n)
}{p \cdot (1 - r) \cdot (1 - r^n)^2}
$$
Analogously, $E_k(T:X_T=0)$ can be expressed as:$$
r^k \cdot \frac{
    n \cdot (1 - r^{n - k}) \cdot (1 + r^n)
  - (n - k) \cdot (1 + r^{n - k}) \cdot (1 - r^n)
}{p  \cdot (1 - r) \cdot (1 - r^n)^2}
$$
Summing both yields:$$
\frac{n \cdot (1 - r^k) - k \cdot (1 - r^n)}{p \cdot (1 - r) \cdot (1 - r^n)}
$$
which is the well-known expression for $E_k(T)$, as expected.
A: 
The usual approach:

For every $0\leqslant k\leqslant n$, let $u_k=E_k(T:X_T=n)$ and $v_k=P_k(X_T=n)$, then $(u_k)$ and $(v_k)$ are determined by the fact that $u_0=u_n=v_0=0$, $v_n=1$, and, for every $1\leqslant k\leqslant n-1$, 
$$
u_k=v_k+pu_{k+1}+qu_{k-1},\qquad
v_k=pv_{k+1}+qv_{k-1}.
$$
The $(v_k)$ system is solved using the usual characteristic equation approach. When $p\ne q$, this yields, for every $0\leqslant k\leqslant n$, 
$$
v_k=\frac{1-r^k}{1-r^n},\qquad r=\frac{q}p.
$$
Plugging this into the $(u_k)$ system and fiddling a little bit with particular solutions such as $u_k=k$, $u_k=r^k$ and $u_k=kr^k$, one can solve for $(u_k)$ and finally deduce the value of 
$$
E_k(T\mid X_T=n)=\frac{u_k}{v_k}.
$$
The case $p=q=\frac12$ is degenerate since $r=1$. Then, $v_k=\frac{k}n$ and,  trying particular solutions such as $u_k=k$, $u_k=k^2$ and $u_k=k^3$, one gets $u_k=\frac13nk-\frac1{3n}k^3$, hence
$$
E_k(T\mid X_T=n)=\tfrac13(n^2-k^2).
$$
A: The answer given only provides an explicit solution for the case p=q=.5
Here is a general solution to the above problem.  You can plug in the gamblers starting amount,  the final amount, the bias of the coin and get a numerical answer for  exactly how many tosses on average it takes for the gambler to win the game.
(I will submit the derivation of my solution when i get my latex skills working again.)
Statement of the Problem in plain english,
A coin is biased to come up heads with probability p , where p is between 0 and 1.
If a gambler starts with m  and bets heads each toss.  The game ends when the
gambler has gone broke (lost m) ,  or has attained n ( won  [n-m] )
PROBLEM- Given, p, m, n   - what is Tw(m,n,p).
        compute the  average length of a WON game.
first,  note that we only need to solve for the gambler starting out with 1,  (m=1) since the time to win  n starting with 1  is equal to the time to win m starting with 1 plus the time to win (n-m) starting with m.   i.e. Tw(m,n,p)= Tw(1,n,p)-Tw(1,m,p)

So we'll only set out to solve for the case m=1,  which by simple subtraction will give us the general case for any  m  from 1 to n-1.
For a biased coin,  Call the probability of winnng the coin toss = p,
and the probability of losing the toss is (1-p) = q,  and call z = q/p. 
A gambler starts with 1, and tosses for stake =1  until he has 0 (ruin) or has n (Wins).
The probability of the gambler winning,  Pwin(n) is  known to be
                 Pwin(n) =  (1-z)/(1-z^n).
    The average time Twinlose(n) of a game (win OR lose) is a  known function of z and n.
   Twinlose(n)=  [n*Pwin(n)-1]/(p-q)
Twin(n) defined as the average time it takes the gambler to WIN such a game. I calculated Twin(n) and found it to be
 Twin(n) = Pwin(n) * (the sum over m from 0 to n of X(n,m) * z^m )

where X(n,m) is the given by the m-th entry of the  n-th row of this sequence.
     X(n,m) = n + 2*m*(n - m) (0 <= m <= n).

The first few rows of this sequence are-
n=1   - 1, 1   -
n=2   - 2,  4,  2  -
n=3  -  3,  7,  7,  3  -
n=4   - 4,  10, 12, 10, 4  -
n=5   -  5,  13, 17, 17, 13, 5  -
n=6   -  6,  16, 22, 24, 22, 16, 6 -
...
