Just because something would improve chances of reproducing, doesn't mean it has actually developed.
If the worm can grow its head back while losing all memories, it has better reproductive chances than if the head doesn't grow back. That's enough for the head-growing-back to develop, there's absolutely no need to also require memories to stay available.
Indeed, a ‘fresh’ head with a blank slate might be preferable….. after all, the original head with its memories and individualized preferences resulted in it losing its head in the first place!