class Solution { LinkedList<String> res = new LinkedList<>(); Map<String,PriorityQueue<String>> map = new HashMap<>(); public List<String> findItinerary(List<List<String>> tickets) { for(List<String> t : tickets) { String f = t.get(0), to = t.get(1); map.computeIfAbsent(f,k->new PriorityQueue<String>()).add(to); } dfs("JFK"); return res; } public void dfs(String curr) { while (map.containsKey(curr) && !map.get(curr).isEmpty()) { String tmp = map.get(curr).poll(); dfs(tmp); } res.addFirst(curr); } // [["JFK","KUL"],["JFK","NRT"],["NRT","JFK"]] }