Java Solution for Destination City Problem
Intuition\n Describe your first thoughts on how to solve this problem. \n\u2022\tThe program utilizes a HashMap to map the paths from each city to its destinati
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\u2022\tThe program utilizes a HashMap to map the paths from each city to its destination city.\n\u2022\tIt then starts from the first destination in the list and iterates through the HashMap until it finds a city without any outgoing path, which represents the destination city.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\u2022\tThe solution uses a HashMap to store the paths where the key is the starting city, and the value is the destination city.\n\u2022\tIt iterates through the list of paths to create this mapping.\n\u2022\tIt starts from the first destination city and follows the paths until it reaches a city with no outgoing path.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\u2022\tThe time complexity of creating the HashMap from the list of paths is O(n), where \u2018n\u2019 is the number of paths given.\n\u2022\tTraversing through the HashMap to find the destination city takes O(n) in the worst case, where \u2018n\u2019 is the number of paths.\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nThe space complexity of the HashMap is O(n), where \u2018n\u2019 is the number of paths, as it stores the paths from each city to its destination.\n\n# Code\n```\nclass Solution {\n    public String destCity(List<List<String>> paths) {\n        Map<String, String> map = new HashMap<>();\n        \n        for (List<String> path : paths) {\n            map.put(path.get(0), path.get(1));\n        }\n        \n        String destination = paths.get(0).get(1);\n        \n        while (map.containsKey(destination)) {\n            destination = map.get(destination);\n        }\n        \n        return destination;\n    }\n}\n```
['Java']