Watch tutorials and explanations by expert teachers that walk through solving this problem.
NeetCode
NeetCodeIO
Programming with Mosh
Adventures in Nodeland - Matteo Collina
Solve more problems to master this pattern