Solution (source code)

= Solution

Let $E_j$ be the expected additional number of tosses when the current terminal run contains $j$ consecutive heads. Then $E_k=0$, and for $0\leq j<k$,
$$
E_j=1+pE_{j+1}+(1-p)E_0.
$$
For $0<p<1$, iterating this recurrence from $j=k-1$ down to $0$ gives
$$
E_0=\bigl(1+(1-p)E_0\bigr)
\frac{1-p^k}{1-p}.
$$
Solving,
$$
\boxed{
E_0=\frac{1-p^k}{(1-p)p^k}}.
$$
When $p=1$, the waiting time is deterministically $k$, which is also the continuous limit of this formula as $p\to1$.