Instead of making a visited list that contains each of the visited nodes, you can make each node hold a boolean that determines if it is visited or not. This can save space, and might make it easier in different problems. Of course, you would need to reset the value if you iterate over the same graph multiple times, but it is something to think about.
Instead of making a visited list that contains each of the visited nodes, you can make each node hold a boolean that determines if it is visited or not. This can save space, and might make it easier in different problems. Of course, you would need to reset the value if you iterate over the same graph multiple times, but it is something to think about.