Prove that if a problem L can be decided in polynomial time, then L ≤p L' for any other problem L' So we know that there exists a Turing Machine $M$ and a polynomial $T$ such that:


$M$ halts on all inputs within at most $T(|x|)$ steps
If $x$ is in $L$ then $M$ accepts $x$
If $x$ is not in $L$ then $M$ rejects $x$


We need to show that for any other problem $L'$, there exists a polynomial time computable function $f$ such that for all $x$, $f(x)$ is in $L'$ if and only if $x$ is in $L$.

I imagine the answer is simple but I'm stumped.
We show a karp reduction from $L$ to any other language $L'$, as long as $L'\notin\{\Sigma^*,\emptyset\}$.

The reduction proceeds as follows. Since $L'$ is non-trivial, there exists $x,y$ such that $x\in L'$ and $y\notin L'$.
Given input $w$, the reduction runs $M$ on $w$ (which takes polynomial time). Then, if $w\in L$ the reduction outputs $x$, and if $w\notin L$ it outputs $y$.

Proving correctness is trivial.

As @dkuper states, this is simply not true if we allow $L'$ to be trivial. However, it is a common mistake to ignore these end-cases in exercises (which I assume this is).