Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #332: Reconstruct Itinerary

In this guide, we solve Leetcode #332 Reconstruct Itinerary in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

You are given a list of airline tickets where tickets[i] = [fromi, toi] represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.

Quick Facts

  • Difficulty: Hard
  • Premium: No
  • Tags: Depth-First Search, Graph, Eulerian Circuit

Intuition

The data forms a graph, so we should explore nodes and edges systematically.

A traversal ensures we visit each node once while maintaining the needed state.

Approach

Build an adjacency list and traverse with BFS or DFS.

Aggregate results as you visit nodes.

Steps:

  • Build the graph.
  • Traverse with BFS/DFS.
  • Accumulate the required output.

Example

Input: tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]] Output: ["JFK","MUC","LHR","SFO","SJC"]

Python Solution

class Solution: def findItinerary(self, tickets: List[List[str]]) -> List[str]: def dfs(f: str): while g[f]: dfs(g[f].pop()) ans.append(f) g = defaultdict(list) for f, t in sorted(tickets, reverse=True): g[f].append(t) ans = [] dfs("JFK") return ans[::-1]

Complexity

The time complexity is O(m×log⁡m)O(m \times \log m)O(m×logm), and the space complexity is O(m)O(m)O(m). The space complexity is O(m)O(m)O(m).

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy