Find shortest path in undirected graph that goes through all vertices and returns to starting vertex
Travelling salesman problem looks almost the same but I can't repeat a visited vertex.

Going from vertex 3 to vertex 5 I can't do that
3 -> 2 -> 3 -> 1 -> 3-> 4 -> 3

A solution is: 3->1->2->4->3 

That's my function: 

private static List&lt;HashMap&lt;Integer,Integer&gt;&gt; graph = new ArrayList&lt;&gt;();


public static void dfs(int currentVertex,Stack&lt;Integer&gt; visited,long currentTime) {
    if(visited.size() == graph.size()-1) {
        if(!graph.get(currentVertex).containsKey(targetVertex)) {
            return;
        }

        currentTime += graph.get(currentVertex).get(targetVertex);
        if(currentTime &lt; fastestTime) {
            fastestTime = currentTime;
        }
        return;
    }

    for (Map.Entry&lt;Integer,Integer&gt; vertex: graph.get(currentVertex).entrySet()) {
        if(!visited.contains(vertex.getKey())) {
            visited.add(vertex.getKey());
            dfs(vertex.getKey(),visited,currentTime + vertex.getValue());
            visited.pop();
        }
    }
}