I am trying to compute the $E[X^2]$ where $X$ is a geometric random variable. I computed $E[X]$ using the logic that $X$ can either be 1 (meaning we get a success on the first try) with probability $p$, or we fail the first try, in which case, we have wasted a try and will have to start over, so we have
\begin{align} E[X] = 1*p + (1-p)*(1+E[X]) \\ E[X] - (1-p)*(1+E[X]) = p \\ -1 + p +pE[X] = p \\ \therefore E[X] = \frac{1}{p} \end{align}
And then I tried to do the following $$ E[X^2] = 1^2 * p + (1-p)*(1+E[X])^2 = p + (1-p)(1+2E[X]+E[X]^2) $$ But this step seems to be wrong, and it seems the correct way would be $$ E[X^2] = 1^2*p + (1-p)(1+2E[X]+E[X^2]) \neq p + (1-p)(1+2E[X]+E[X]^2) $$
Why is my former approach not correct? I thought I correctly applied the law of the unconscious statistician: $$ E[g(x)] = \sum_{x} g(x) f(x) \\ $$ In this case $g(x) = x^2$, so couldn't I simply square the $(1+E[X])$ term?
Here are the details for evaluating $E[X]$:
$$\begin{align*}\sum_{x=1}^\infty x f(x) &= 1\cdot p + (1-p)\sum_{x=1}^\infty (x+1) f(x) \\ &= 1\cdot p + (1-p)\left(\sum_{x=1}^\infty f(x) + \sum_{x=1}^\infty xf(x)\right) \\ &= 1 \cdot p + (1-p)\left(1 + \sum_{x=1}^\infty x f(x)\right)\end{align*}$$
Here are the details for evaluating $E[X^2]$: $$\begin{align*}\sum_{x=1}^\infty x^2 f(x) &= 1^2\cdot p + (1-p)\sum_{x=1}^\infty (x+1)^2 f(x) \\ &= 1^2\cdot p + (1-p)\left(\sum_{x=1}^\infty f(x) + 2\sum_{x=1}^\infty xf(x) + \sum_{x=1}^\infty x^2f(x)\right) \\ &= 1^2 \cdot p + (1-p)\left(1 + 2\sum_{x=1}^\infty x f(x) + \sum_{x=1}^\infty x^2f(x)\right)\end{align*}$$
Note that you can never bring the square on $x^2$ out of the sum.
Here, in both cases we've used $f(x+1) = (1-p)f(x)$ for the geometric distribution. Other than that, it's just reparametrization and algebra.