This is all going off the idea that Sammy was a real person and not a projection of Lenny’s.
Your question: “How did Leonard manage to distort his memory of Sammy Jankins?”
This is sort of a theme throughout the whole movie...that memories are unreliable and cannot be trusted (especially in Lenny’s case.) The “memory” of Sammy Jankins isn’t a memory at all, it’s a story that Lenny created to ease the guilt and pain of knowing that he actually killed his wife. Therefore he can distort it however he wants since he created it.
If you look closely at the scene where Lenny is on the phone talking about Sammy, it will show Sammy sitting in a chair in the hospital, just as someone passes by the cameras view it will actually show Lenny himself sitting in the chair, not Sammy. (around 130:00 minutes). This indicates that Sammy and Lenny are actually one and the same.
Lenny created the story about Sammy to “condition” himself into forgetting he was the one who actually killed his wife from insulin injections. The tattoo “remember Sammy Jankins” is Lenny’s trigger for himself. During the closing scene Teddy ask Lenny what happened to the 12 missing pages of the file. Those 12 pages included all the facts I just stated, that Lenny was actually the one who killed his own wife.
Lenny destroys these pages (and won’t remember doing it) so that he can create this perpetual scavenger hunt for “John G”, the man who Lenny has conditioned himself into thinking actually killed his wife (Which fits perfect into Lenny’s actual memories of his wife, her laying on the bathroom floor with plastic over her head.)
This continuous hunt for “John G” gives purpose to Lenny’s life, a life of 5 minutes at a time. The film opens with Lenny killing Teddy, at the end when Lenny writes down “fact 6” and puts down Teddy’s license plate number, in that moment he is putting Teddy’s death in motion, as he will hunt Teddy thinking that he is “John G”.
The scene with Lenny and his wife in bed, and the tattoo on his chest “ I have done it” can be seen, is another case where it isn’t a memory. He wouldn’t have the ability to remember that event as it would have taken place after the attack had happened which created his memory loss.
So many years later and the movie is still talked about and awesome to rewatch. Nolan at his finest.