My Python solutions to LeetCode problems, organized by topic along the NeetCode roadmap. Solutions are kept exactly as submitted to LeetCode, and every one is tested against the problem's examples.
Space complexity excludes the output. h is the height of the tree.
| # | Problem | Difficulty | Solution | Time | Space |
|---|---|---|---|---|---|
| 1 | Two Sum | Easy | Python | O(n log n) |
O(n) |
| 13 | Roman to Integer | Easy | Python | O(n) |
O(1) |
| 14 | Longest Common Prefix | Easy | Python | O(n * m) |
O(m) |
| # | Problem | Difficulty | Solution | Time | Space |
|---|---|---|---|---|---|
| 26 | Remove Duplicates from Sorted Array | Easy | Python | O(n) |
O(1) |
| 27 | Remove Element | Easy | Python | O(n) |
O(n) |
| 88 | Merge Sorted Array | Easy | Python | O(m + n) |
O(1) |
| 125 | Valid Palindrome | Easy | Python | O(n) |
O(1) |
| # | Problem | Difficulty | Solution | Time | Space |
|---|---|---|---|---|---|
| 3 | Longest Substring Without Repeating Characters | Medium | Python | O(n) |
O(min(n, k)) |
| 121 | Best Time to Buy and Sell Stock | Easy | Python | O(n) |
O(1) |
| # | Problem | Difficulty | Solution | Time | Space |
|---|---|---|---|---|---|
| 20 | Valid Parentheses | Easy | Python | O(n) |
O(n) |
| # | Problem | Difficulty | Solution | Time | Space |
|---|---|---|---|---|---|
| 35 | Search Insert Position | Easy | Python | O(log n) |
O(1) |
| 69 | Sqrt(x) | Easy | Python | O(1) |
O(1) |
| # | Problem | Difficulty | Solution | Time | Space |
|---|---|---|---|---|---|
| 2 | Add Two Numbers | Medium | Python | O(max(n, m)) |
O(1) |
| 21 | Merge Two Sorted Lists | Easy | Python | O(n + m) |
O(1) |
| 83 | Remove Duplicates from Sorted List | Easy | Python | O(n) |
O(1) |
| 141 | Linked List Cycle | Easy | Python | O(n) |
O(1) |
| # | Problem | Difficulty | Solution | Time | Space |
|---|---|---|---|---|---|
| 94 | Binary Tree Inorder Traversal | Easy | Python | O(n) |
O(h) |
| 100 | Same Tree | Easy | Python | O(n) |
O(h) |
| 101 | Symmetric Tree | Easy | Python | O(n) |
O(h) |
| 104 | Maximum Depth of Binary Tree | Easy | Python | O(n) |
O(h) |
| 108 | Convert Sorted Array to Binary Search Tree | Easy | Python | O(n) |
O(log n) |
| 110 | Balanced Binary Tree | Easy | Python | O(n) |
O(h) |
| 111 | Minimum Depth of Binary Tree | Easy | Python | O(n) |
O(h) |
| 112 | Path Sum | Easy | Python | O(n) |
O(h) |
| # | Problem | Difficulty | Solution | Time | Space |
|---|---|---|---|---|---|
| 70 | Climbing Stairs | Easy | Python | O(n) |
O(n) |
| 118 | Pascal's Triangle | Easy | Python | O(n^2) |
O(1) |
| 119 | Pascal's Triangle II | Easy | Python | O(n^2) |
O(1) |
| # | Problem | Difficulty | Solution | Time | Space |
|---|---|---|---|---|---|
| 455 | Assign Cookies | Easy | Python | O(n log n + m log m) |
O(n + m) |
| # | Problem | Difficulty | Solution | Time | Space |
|---|---|---|---|---|---|
| 7 | Reverse Integer | Medium | Python | O(log x) |
O(1) |
| 9 | Palindrome Number | Easy | Python | O(log x) |
O(1) |
| # | Problem | Difficulty | Solution | Time | Space |
|---|---|---|---|---|---|
| 67 | Add Binary | Easy | Python | O(n) |
O(n) |
| 136 | Single Number | Easy | Python | O(n) |
O(1) |
.
├── solutions/ # one folder per topic, e.g. solutions/trees/0104_maximum_depth_of_binary_tree.py
└── tests/ # pytest cases for every solution, one file per topic
Requires Python 3.10+.
pip install -r requirements-dev.txt
pytestThe tests run each solution with the names LeetCode provides without imports
(List, Optional, deque, ListNode, TreeNode, ...), so solution files
don't need any changes to run locally.
- Paste the accepted submission, unchanged, into
solutions/<topic>/<number>_<slug>.py. Pad the number to four digits and take the slug from the LeetCode URL, for examplesolutions/arrays-and-hashing/0217_contains_duplicate.py. New topic folders follow NeetCode's category names, such asheap-priority-queue,backtracking, orgraphs. - Add a row for it to the matching table above.
- Add a
test_<number>_<slug>test totests/test_<topic>.py.
CI fails if a solution is missing from the README, a row's link doesn't match its file name, or a solution has no test.