Let $a$ and $b$ be real numbers, and $g(x)=a x+b$. Determine the polynomials $f$ such that $f(g(x))=g(f(x))$ for all $x \in \mathbb{R}$.
First case: $a=1$ and $b=0$. Then any polynomial $f$ works.

Second case: $a=1$ and $b \neq 0$. Since $f(x+b)=f(x)+b$, it is easy to show by induction that $f(n b)=f(0)+n b$ for any natural number $n$, hence the polynomial $x \mapsto f(b x)-f(0)-b x$ has an infinite number of roots. Consequently, it is zero, so $f(t)=t+f(0)$ for all $t$. We conclude that $f(x)$ is of the form $f(x)=x+c$ where $c$ is a constant.

Third case: $a \neq 1$. Then $g$ has a unique fixed point $c$ (namely $c=$ $b /(1-a))$. We verify that $g(x)-c=a(x-c)$.

Let $y=x-c$ and $h(y)=f(y+c)-c$. Then the condition $f(g(x))=g(f(x))$ becomes $h(a y)=a h(y)$.

If $a \neq-1$, then we have $h\left(a^{k}\right)=a^{k} h(1)$ for all integers $k \geqslant 1$, so $y \mapsto$ $h(y)-h(1) y$ has an infinite number of roots. This implies $h(y)=\lambda y$ for some constant $\lambda$, and finally $f(x)=c+\lambda(x-c)$.

If $a=-1$, the condition reads $h(-y)=-h(y)$, meaning that $h$ is an odd polynomial (which means that the coefficients of the even-degree terms are zero).