The expected number of tosses of a fair coin until one achieves 5 heads in a row has a very neat solution using generating functions.
In particular, let $N$ be a random variable counting the number of tosses until 5 heads are obtained. Then the generating function is given by: \begin{align*} f_N(x) = E(x^N)&=\sum_{n=5}^\infty P(N=n)x^n\\ &= \sum_{k=0}^{\infty}\left(\frac{x}{2}+\frac{x^2}{4}+\frac{x^3}{8}+\frac{x^4}{16}+\frac{x^5}{32}\right)^k\frac{x^5}{32} \end{align*} I can see intuitively where this is coming from and how the coefficient of the corresponding powers yield the correct probability in simple cases, but I wish to show it from first principles. I've tried the following: let there be $K$ substrings of failing tosses delimited by $T$ for example $T$, $HT$, $HHT$, $HHHT$, $HHHHT$ (these are bad strings which make us start the process again). Let $1\leq Y_i\leq 5 \quad i\in\{1,\cdots,K\}$ be the length of each such substring, see the example below for a sample draw.
Conditioning on $K$ we have the following $$ f_N(x) = \sum_{k=0}^\infty E(x^N\mid K=k)P(K=k) $$ Now, given $k$ bad substrings, each of length $Y_i$, the total length of our string is given by $$ N_k = \sum_{j=1}^kY_j + Z $$ Where $P(Z=5)=1$. Since the $Y_i$ are independent and the generating function of a sum of independent variables is the product of generating function we have: \begin{gather} E(x^N\mid K=k) = f_{N_k}(x) = \left(\frac{x}{2}+\frac{x^2}{4}+\frac{x^3}{8}+\frac{x^4}{16}+\frac{x^5}{32}\right)^kx^5. \end{gather} Putting it all together, noting that $$ P(K=k) = \left(1-\frac{1}{32}\right)^k\frac{1}{32} $$ \begin{equation*} f_N(x) = \sum_{k=0}^{\infty}\left(\frac{x}{2}+\frac{x^2}{4}+\frac{x^3}{8}+\frac{x^4}{16}+\frac{x^5}{32}\right)^k\frac{x^5}{32}\left(1-\frac{1}{32}\right)^k \end{equation*} which is not quite the same thing, in particular I can't see where my logic falls apart.
Example
The following scenario $$ \{HT\} \{T\} \{HHT\} \{HHHHT\} \{HHHHH\} $$ Would have $K=4$, $Y_1=2$, $Y_2=1$, $Y_3=3$, $Y_4=5$ and $N=2+1+3+5+5=16$.