🧠5 LeetCode Problems Explained with Code + Video
Part of the DSA Decoded series — step-by-step Python solutions with video walkthroughs.
Problems covered in this post:
- 🟢 Reverse Linked List (LeetCode #206) · Easy · Recursion
- 🟡 Subsets (LeetCode #78) · Medium · Backtracking
- 🟡 Search in Rotated Sorted Array (LeetCode #33) · Medium · Binary Search
- 🟡 Koko Eating Bananas (LeetCode #875) · Medium · Binary Search
- 🟡 Merge Intervals (LeetCode #56) · Medium · Intervals
🟢 Reverse Linked List
Difficulty: Easy  | Topic: Recursion  | LeetCode #206
Problem
Given the head of a singly linked list, reverse the list, and return the reversed list. Solve it both iteratively and recursively.
Solution
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def reverse_list(head):
if not head or not head.next:
return head
new_head = reverse_list(head.next)
head.next.next = head
head.next = None
return new_head
🔗 Solve on LeetCode  · 💻 Full solution + tests on GitHub
🟡 Subsets
Difficulty: Medium  | Topic: Backtracking  | LeetCode #78
Problem
Given an integer array nums of unique elements, return all possible subsets (the power set). The solution set must not contain duplicate subsets. Return the solution in any order.
Solution
def subsets(nums):
result = []
def backtrack(start, current):
result.append(current[:])
for i in range(start, len(nums)):
current.append(nums[i])
backtrack(i + 1, current)
current.pop()
backtrack(0, [])
return result
🔗 Solve on LeetCode  · 💻 Full solution + tests on GitHub
🟡 Search in Rotated Sorted Array
Difficulty: Medium  | Topic: Binary Search  | LeetCode #33
Problem
There is an integer array nums sorted in ascending order (with distinct values). Prior to being passed to your function, nums is possibly rotated at an unknown pivot index k. Given the array nums after the possible rotation and an integer target, return the index of target if it is in nums, or -1 if it is not in nums. You must write an algorithm with O(log n) runtime complexity.
Solution
def search(nums, target):
left, right = 0, len(nums) - 1
while left <= right:
mid = (left + right) // 2
if nums[mid] == target:
return mid
if nums[left] <= nums[mid]:
if nums[left] <= target < nums[mid]:
right = mid - 1
else:
left = mid + 1
else:
if nums[mid] < target <= nums[right]:
left = mid + 1
else:
right = mid - 1
return -1
🔗 Solve on LeetCode  · 💻 Full solution + tests on GitHub
🟡 Koko Eating Bananas
Difficulty: Medium  | Topic: Binary Search  | LeetCode #875
Problem
Koko loves to eat bananas. There are n piles of bananas, the i-th pile has piles[i] bananas. The guards have gone and will come back in h hours. Koko can decide her bananas-per-hour eating speed of k. Each hour, she chooses some pile of bananas and eats k bananas from that pile. She can finish the pile in one sitting if it's smaller. Return the minimum integer k such that she can eat all the bananas within h hours.
Solution
import math
def min_eating_speed(piles, h):
left, right = 1, max(piles)
result = right
while left <= right:
k = (left + right) // 2
hours = sum(math.ceil(p / k) for p in piles)
if hours <= h:
result = k
right = k - 1
else:
left = k + 1
return result
🔗 Solve on LeetCode  · 💻 Full solution + tests on GitHub
🟡 Merge Intervals
Difficulty: Medium  | Topic: Intervals  | LeetCode #56
Problem
Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.
Solution
def merge(intervals):
intervals.sort(key=lambda x: x[0])
merged = [intervals[0]]
for start, end in intervals[1:]:
if start <= merged[-1][1]:
merged[-1][1] = max(merged[-1][1], end)
else:
merged.append([start, end])
return merged
🔗 Solve on LeetCode  · 💻 Full solution + tests on GitHub
🔔 Follow Along
New problems every 2 days — subscribe to stay consistent with your DSA prep!
Drop a comment with your approach — always curious to see other solutions!
Top comments (0)