Explained | Easy & Understandable Approach | Java
Intuition\n Describe your first thoughts on how to solve this problem. \nMy first thoughts were to create a HashMap and iterate though it till a key that is use
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nMy first thoughts were to create a HashMap and iterate though it till a key that is used returns null.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nWe first define a HashMap and put the paths into it where path - key is the starting city and value is the ending city.\nThat means that the key and value is the first and second element of each list in the list respectively.\n\nWe then create a String $$x$$ that has the first element of the first list in the list.\n\nThen we iterate through the map using $$x$$ with a while loop.\nWe keep updating x with the value used when x is passed as a key.\nThis goes on till the value of $$x$$ returns $$null$$ when used as a key.\n\nThis means $$x$$ is a city without any path going to an outgoing city.\nTherefore, we return $$x$$. \n\n# Complexity\n- Time complexity: $$O(N+M)$$ where N is the number of paths and M is the number of cities.\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(M)$$ where M is the number of cities.\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public String destCity(List<List<String>> paths) {\n        HashMap<String,String> map = new HashMap<>();\n        for(List<String> x:paths){\n            map.put(x.get(0),x.get(1));\n        }\n    String x=paths.get(0).get(0);\n    while(map.get(x)!=null){\n        x=map.get(x);\n    }\n    return x;\n    }\n}\n```
['Hash Table', 'Java']