If you have a solid understanding of what the algorithm does and how it does it, then it becomes trivially easy to memorize the corresponding code, or just rewrite it from scratch, because you know what it's meant to be doing.
Also spaced repetition, come back to it every few days until you've got it all down.