- Create a map
graph
to store the adjacency list of the graph, where the keys are the airports and the values are priority queues of the next airports that can be visited from the current airport. - Iterate over the
tickets
list and add each ticket to thegraph
map. - Create a linked list
itinerary
to store the itinerary. - Call the
dfs()
function with the starting airport, thegraph
map, and theitinerary
linked list.
The dfs()
function works recursively to find a path from the current airport to the end of the itinerary. It does this by repeatedly removing the next airport from the priority queue and calling itself on that airport. When the priority queue is empty, the function adds the current airport to the itinerary
linked list.
Example:
Consider the following input:
tickets = [[["JFK", "SFO"], ["JFK", "ATL"], ["SFO", "ATL"], ["ATL", "JFK"]]]
The output is ["JFK", "ATL", "JFK", "SFO"]
.
Explanation:
The graph
map after the iteration over the tickets
list is as follows:
graph = { "JFK": ["ATL", "SFO"], "ATL": ["JFK"], "SFO": ["ATL"] }
The dfs()
function is called with the starting airport "JFK" as follows:
dfs("JFK", graph, itinerary);
The dfs()
function will recursively call itself on the next airports in the priority queue of "JFK", which are "ATL" and "SFO". The dfs()
function will eventually reach the end of the itinerary and return. The itinerary
linked list will then contain the following airports in order:
["JFK", "ATL", "JFK", "SFO"]
Therefore, the output of the findItinerary()
function is ["JFK", "ATL", "JFK", "SFO"]
.