100 LeetCode Python Solutions: Interview Study Guide

Study 100 essential LeetCode interview problems with original Python solutions, pattern explanations, correctness arguments, complexity analysis, and edge cases. The main collection follows LeetCode’s official Top Interview study plan and adds the site’s earlier worked solutions.

How to use this library

  1. Choose a topic or difficulty in the searchable table below.
  2. Open the official LeetCode prompt from the solution page and attempt it before reading the code.
  3. Identify the reusable pattern: two pointers, sliding window, monotonic reasoning, graph traversal, topological sort, or another invariant.
  4. Explain the complexity and correctness argument aloud before submitting.
  5. Return later and implement the solution without looking.

Patterns worth mastering

PatternRecognize it when…
Two pointersInput is sorted, endpoints constrain an optimum, or values must be compacted in place.
Sliding windowA contiguous range must satisfy a count, sum, or uniqueness condition.
Hash map / setFast membership, complements, frequencies, or previously seen states matter.
StackNested structure, nearest unmatched items, or expression evaluation is involved.
Tree DFS / BFSThe answer decomposes by subtree or by level.
Graph traversalObjects and relationships form connected components, paths, or dependency cycles.
TrieMany word or prefix searches share the same character paths.

Search all solutions

103 solutions shown

#Problem and solutionDifficultyTopicPattern
1 LeetCode 1: Two Sum — Python Solution Easy Hashmapcomplement lookup
2 LeetCode 2: Add Two Numbers — Python Solution Medium Linked Listdigitwise carry
3 LeetCode 3: Longest Substring Without Repeating Characters — Python Solution Medium Sliding Windowlast-seen sliding window
6 LeetCode 6: Zigzag Conversion — Python Solution Medium Array / Stringrow simulation
11 LeetCode 11: Container With Most Water — Python Solution Medium Two Pointersgreedy boundary movement
12 LeetCode 12: Integer to Roman — Python Solution Medium Array / Stringgreedy token emission
13 LeetCode 13: Roman to Integer — Python Solution Easy Array / Stringlook-ahead parsing
14 LeetCode 14: Longest Common Prefix — Python Solution Easy Array / Stringvertical character scan
15 LeetCode 15: 3Sum — Python Solution Medium Two Pointerssort plus two pointers
19 LeetCode 19: Remove Nth Node From End of List — Python Solution Medium Linked Listfixed-gap pointers
20 LeetCode 20: Valid Parentheses — Python Solution Easy Stackexpected-closer stack
21 LeetCode 21: Merge Two Sorted Lists — Python Solution Easy Linked Listdummy-tail merge
25 LeetCode 25: Reverse Nodes in k-Group — Python Solution Hard Linked Listgroup boundary reversal
26 LeetCode 26: Remove Duplicates from Sorted Array — Python Solution Easy Array / Stringsorted write pointer
27 LeetCode 27: Remove Element — Python Solution Easy Array / Stringwrite pointer
28 LeetCode 28: Find the Index of the First Occurrence in a String — Python Solution Easy Array / Stringsubstring scan
30 LeetCode 30: Substring with Concatenation of All Words — Python Solution Hard Sliding Windowword-aligned windows
36 LeetCode 36: Valid Sudoku — Python Solution Medium Matrixrow-column-box sets
42 LeetCode 42: Trapping Rain Water — Python Solution Hard Array / Stringtwo pointers with bounded sides
45 LeetCode 45: Jump Game II — Python Solution Medium Array / Stringgreedy BFS frontier
48 LeetCode 48: Rotate Image — Python Solution Medium Matrixtranspose then reverse rows
49 LeetCode 49: Group Anagrams — Python Solution Medium Hashmapcanonical frequency key
54 LeetCode 54: Spiral Matrix — Python Solution Medium Matrixshrinking boundaries
55 LeetCode 55: Jump Game — Python Solution Medium Array / Stringgreedy reachability
56 LeetCode 56: Merge Intervals — Python Solution Medium Intervalssorted sweep
57 LeetCode 57: Insert Interval — Python Solution Medium Intervalsthree-phase interval scan
58 LeetCode 58: Length of Last Word — Python Solution Easy Array / Stringreverse scan
61 LeetCode 61: Rotate List — Python Solution Medium Linked Listcycle then cut
68 LeetCode 68: Text Justification — Python Solution Hard Array / Stringgreedy line packing
71 LeetCode 71: Simplify Path — Python Solution Medium Stackcanonical path stack
73 LeetCode 73: Set Matrix Zeroes — Python Solution Medium Matrixfirst-row marker compression
76 LeetCode 76: Minimum Window Substring — Python Solution Hard Sliding Windowdeficit-count window
79 Leetcode 79 – Boggle Game / Word Search Medium Backtrackingdepth-first search with backtracking
80 LeetCode 80: Remove Duplicates from Sorted Array II — Python Solution Medium Array / Stringbounded write pointer
82 LeetCode 82: Remove Duplicates from Sorted List II — Python Solution Medium Linked Listrun skipping
86 LeetCode 86: Partition List — Python Solution Medium Linked Liststable dual lists
88 LeetCode 88: Merge Sorted Array — Python Solution Easy Array / Stringreverse two pointers
92 LeetCode 92: Reverse Linked List II — Python Solution Medium Linked Listhead insertion reversal
98 LeetCode 98: Validate Binary Search Tree — Python Solution Medium Binary Search Treerecursive value bounds
100 LeetCode 100: Same Tree — Python Solution Easy Binary Tree Generalpaired recursion
101 LeetCode 101: Symmetric Tree — Python Solution Easy Binary Tree Generalmirror recursion
102 LeetCode 102: Binary Tree Level Order Traversal — Python Solution Medium Binary Tree BFSbreadth-first levels
103 LeetCode 103: Binary Tree Zigzag Level Order Traversal — Python Solution Medium Binary Tree BFSalternating level output
104 LeetCode 104: Maximum Depth of Binary Tree — Python Solution Easy Binary Tree Generalrecursive postorder
105 LeetCode 105: Construct Binary Tree from Preorder and Inorder Traversal — Python Solution Medium Binary Tree Generalindexed recursive partition
106 LeetCode 106: Construct Binary Tree from Inorder and Postorder Traversal — Python Solution Medium Binary Tree Generalreverse postorder partition
112 LeetCode 112: Path Sum — Python Solution Easy Binary Tree Generalremaining-sum DFS
114 LeetCode 114: Flatten Binary Tree to Linked List — Python Solution Medium Binary Tree Generalreverse preorder threading
117 LeetCode 117: Populating Next Right Pointers in Each Node II — Python Solution Medium Binary Tree Generallevel linked-list construction
121 LeetCode 121: Best Time to Buy and Sell Stock — Python Solution Easy Array / Stringrunning minimum
122 LeetCode 122: Best Time to Buy and Sell Stock II — Python Solution Medium Array / Stringgreedy positive differences
124 LeetCode 124: Binary Tree Maximum Path Sum — Python Solution Hard Binary Tree Generalpostorder gain DP
125 LeetCode 125: Valid Palindrome — Python Solution Easy Two Pointersfiltered two pointers
127 LeetCode 127: Word Ladder — Python Solution Hard Graph BFSwildcard-neighbor BFS
128 LeetCode 128: Longest Consecutive Sequence — Python Solution Medium Hashmapset sequence starts
129 LeetCode 129: Sum Root to Leaf Numbers — Python Solution Medium Binary Tree Generalprefix-number DFS
130 LeetCode 130: Surrounded Regions — Python Solution Medium Graph Generalboundary flood fill
133 LeetCode 133: Clone Graph — Python Solution Medium Graph GeneralDFS memoized cloning
134 LeetCode 134: Gas Station — Python Solution Medium Array / Stringgreedy restart
135 LeetCode 135: Candy — Python Solution Hard Array / Stringtwo directional passes
138 LeetCode 138: Copy List with Random Pointer — Python Solution Medium Linked Listnode-to-copy map
141 LeetCode 141: Linked List Cycle — Python Solution Easy Linked ListFloyd cycle detection
146 LeetCode 146: LRU Cache — Python Solution Medium Linked Listhash map plus doubly linked list
150 LeetCode 150: Evaluate Reverse Polish Notation — Python Solution Medium Stackoperand stack
151 LeetCode 151: Reverse Words in a String — Python Solution Medium Array / Stringtoken normalization
155 LeetCode 155: Min Stack — Python Solution Medium Stackpaired minimum stack
167 LeetCode 167: Two Sum II – Input Array Is Sorted — Python Solution Medium Two Pointersopposing two pointers
169 LeetCode 169: Majority Element — Python Solution Easy Array / StringBoyer-Moore voting
173 LeetCode 173: Binary Search Tree Iterator — Python Solution Medium Binary Tree Generallazy inorder stack
189 LeetCode 189: Rotate Array — Python Solution Medium Array / Stringthree reversals
199 LeetCode 199: Binary Tree Right Side View — Python Solution Medium Binary Tree BFSlevel-order last node
200 LeetCode 200: Number of Islands — Python Solution Medium Graph Generalgrid flood fill
202 LeetCode 202: Happy Number — Python Solution Easy Hashmapcycle detection
205 LeetCode 205: Isomorphic Strings — Python Solution Easy Hashmapbidirectional mapping
207 LeetCode 207: Course Schedule — Python Solution Medium Graph GeneralKahn topological sort
208 LeetCode 208: Implement Trie (Prefix Tree) — Python Solution Medium Trienested child maps
209 LeetCode 209: Minimum Size Subarray Sum — Python Solution Medium Sliding Windowpositive sliding window
210 LeetCode 210: Course Schedule II — Python Solution Medium Graph Generaltopological ordering
211 LeetCode 211: Design Add and Search Words Data Structure — Python Solution Medium Trietrie plus wildcard DFS
212 LeetCode 212: Word Search II — Python Solution Hard Trietrie-guided board DFS
219 LeetCode 219: Contains Duplicate II — Python Solution Easy Hashmaplast-index tracking
222 LeetCode 222: Count Complete Tree Nodes — Python Solution Medium Binary Tree Generalperfect-subtree detection
224 LeetCode 224: Basic Calculator — Python Solution Hard Stacksign-context stack
226 LeetCode 226: Invert Binary Tree — Python Solution Easy Binary Tree Generalrecursive swap
228 LeetCode 228: Summary Ranges — Python Solution Easy Intervalsrun detection
230 LeetCode 230: Kth Smallest Element in a BST — Python Solution Medium Binary Search Treeiterative inorder
236 LeetCode 236: Lowest Common Ancestor of a Binary Tree — Python Solution Medium Binary Tree Generalpostorder target propagation
238 LeetCode 238: Product of Array Except Self — Python Solution Medium Array / Stringprefix and suffix products
242 LeetCode 242: Valid Anagram — Python Solution Easy Hashmapfrequency equality
274 LeetCode 274: H-Index — Python Solution Medium Array / Stringsorting and threshold scan
289 LeetCode 289: Game of Life — Python Solution Medium Matrixencoded in-place transition
290 LeetCode 290: Word Pattern — Python Solution Easy Hashmapbidirectional token mapping
380 LeetCode 380: Insert Delete GetRandom O(1) — Python Solution Medium Array / Stringarray plus index map
383 LeetCode 383: Ransom Note — Python Solution Easy Hashmapfrequency consumption
392 LeetCode 392: Is Subsequence — Python Solution Easy Two Pointerssubsequence pointer
399 LeetCode 399: Evaluate Division — Python Solution Medium Graph Generalweighted graph search
433 LeetCode 433: Minimum Genetic Mutation — Python Solution Medium Graph BFSsingle-character BFS
452 LeetCode 452: Minimum Number of Arrows to Burst Balloons — Python Solution Medium Intervalsgreedy earliest end
530 LeetCode 530: Minimum Absolute Difference in BST — Python Solution Easy Binary Search Treeinorder adjacent difference
637 LeetCode 637: Average of Levels in Binary Tree — Python Solution Easy Binary Tree BFSlevel aggregation
733 Leetcode 733 – FloodFill Easy Graph Generalgrid flood fill
909 LeetCode 909: Snakes and Ladders — Python Solution Medium Graph BFSboard-index BFS
1768 Leetcode 1768 – Merge Strings Alternatively Easy Array / Stringtwo-pointer interleaving

About these answers

Every implementation in the downloadable library is independently written and exercised by the included behavioral test suite. These pages deliberately avoid reproducing LeetCode’s full problem statements or examples; each solution links to the official prompt for the authoritative requirements and submission environment.