File tree 1 file changed +11
-9
lines changed
1 file changed +11
-9
lines changed Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public List <String > findItinerary (List <List <String >> tickets ) {
3
- LinkedList <String > itinerary = new LinkedList <>();
4
- Map <String , PriorityQueue <String >> graph = new HashMap <>();
5
- Stack <String > stack = new Stack <>();
3
+ Map <String , PriorityQueue <String >> map = new HashMap <>();
6
4
for (List <String > ticket : tickets ) {
7
- graph .computeIfAbsent (ticket .get (0 ), k -> new PriorityQueue <>()).add (ticket .get (1 ));
5
+ String from = ticket .get (0 );
6
+ String to = ticket .get (1 );
7
+ map .computeIfAbsent (from , k -> new PriorityQueue <>()).add (to );
8
8
}
9
+ Stack <String > stack = new Stack <>();
10
+ LinkedList <String > result = new LinkedList <>();
9
11
stack .push ("JFK" );
10
12
while (!stack .isEmpty ()) {
11
- String nextDestination = stack .peek ();
12
- if (!graph .getOrDefault (nextDestination , new PriorityQueue <>()).isEmpty ()) {
13
- stack .push (graph .get (nextDestination ). poll ());
13
+ String destination = stack .peek ();
14
+ if (!map .getOrDefault (destination , new PriorityQueue <>()).isEmpty ()) {
15
+ stack .push (map .get (destination ). remove ());
14
16
} else {
15
- itinerary .addFirst (stack .pop ());
17
+ result .addFirst (stack .pop ());
16
18
}
17
19
}
18
- return itinerary ;
20
+ return result ;
19
21
}
20
22
}
You can’t perform that action at this time.
0 commit comments