A Hamiltonian path in a graph is a path that visits all the nodes/vertices exactly once, a hamiltonian cycle is a cyclic path, i.e. all nodes visited once and the start and the endpoint are the same.
If we want to solve the snake game using this, we could divide the playable space in a grid and then try to just keep traversing on a hamiltonian cycle, this means you would eventually get all the rewards and never hit yourself unless you are longer than can fit on the screen.
You can look at the code given in this Github repo.
Note that this method, as you can tell, is not going to be optimal by time, there could be better solutions than this.
You will have to use a graph to store all the nodes and the complexity is supposed to be O(n!).