Ratios $4 : k$ and $k : 9$ are equivalent $\Rightarrow k = 6$
(if $k=6$ isn't obvious you can get it from $\frac4k=\frac k9\Rightarrow k^2 = 4\times9$)
So journeys are $6+9$ and $6+4$ hours long.
Using fractions of the total journey times
The fraction of the journey that the red train completes in $k$ hours is the same as the fraction of the journey that the blue train completes in 4 hours.
$$\begin{align}\dfrac k{k+9}&=\dfrac 4 { k+4}\\
\Rightarrow k (k+4)&=4(k+9)\\ \Rightarrow k^2+4k&=4k+36\\ \Rightarrow k^2 &= 36\\
\Rightarrow k &=6\end{align}$$ So journeys are $6+9$ and $6+4$ hours long.
Using distance = speed $\times$ time
Suppose that the train from A to B travels at $a$ km per hour, and the train from B to A travels at $b$ km per hour. Then this diagram shows the distances travelled by each train in each part of the journey:
So $ak=4b$, and $9a=bk$.
We can make $a$ (or $b$) the subject of the first equation, and substitute it into the second: $$\begin{align}a&=\dfrac{4b}{k}\\ \Rightarrow 9\frac{4b}k &= bk\\
\Rightarrow 9\times 4b &= b\times k^2\\
\Rightarrow 36 &= k^2\end{align}$$
So $k=6$, which means the journey from A to B is $15$ hours long, and the journey from B to A is $10$ hours long.