Having solved more than 1500 LeetCode problems, if there is one thing I have learned, it’s this:
LeetCode is less about the number of problems you have solved and more about how many patterns you know.
Learning patterns enable you to solve a wide variety of problems in less time and help you quickly identify the right approach to a problem you have never seen before.

In this article, I’ll walk you through the 15 most important patterns I learned that made my LeetCode journey lot less painful.
I’ll share when to use each pattern along with a sample problem and provide links to LeetCode problems you can practice to learn these patterns better.
1. Prefix Sum

Prefix Sum involves preprocessing an array to create a new array where each element at index i represents the sum of the array from the start up to i. This allows for efficient sum queries on subarrays.
Use this pattern when you need to perform multiple sum queries on a subarray or need to calculate cumulative sums.
Sample Problem:
Given an array nums, answer multiple queries about the sum of elements within a specific range [i, j].
Example:
Input:
nums = [1, 2, 3, 4, 5, 6],i = 1,j = 3Output:
9
Explanation:
Preprocess the array
Ato create a prefix sum array:P = [1, 3, 6, 10, 15, 21].To find the sum between indices
iandj, use the formula:P[j] - P[i-1].
LeetCode Problems:
2. Two Pointers

The Two Pointers pattern involves using two pointers to iterate through an array or list, often used to find pairs or elements that meet specific criteria.
Use this pattern when dealing with sorted arrays or lists where you need to find pairs that satisfy a specific condition.
Sample Problem:
Find two numbers in a sorted array that add up to a target value.
Example:
Input:
nums = [1, 2, 3, 4, 6],target = 6Output:
[1, 3]
Explanation:
Initialize two pointers, one at the start (
left) and one at the end (right) of the array.Check the sum of the elements at the two pointers.
If the sum equals the target, return the indices.
If the sum is less than the target, move the left pointer to the right.
If the sum is greater than the target, move the right pointer to the left.
LeetCode Problems:
3. Sliding Window

The Sliding Window pattern is used to find a subarray or substring that satisfies a specific condition, optimizing the time complexity by maintaining a window of elements.
Use this pattern when dealing with problems involving contiguous subarrays or substrings.
Sample Problem:
Find the maximum sum of a subarray of size k.
Example:
Input:
nums = [2, 1, 5, 1, 3, 2],k = 3Output:
9
Explanation:
Start with the sum of the first
kelements.Slide the window one element at a time, subtracting the element that goes out of the window and adding the new element.
Keep track of the maximum sum encountered.
LeetCode Problems:
4. Fast & Slow Pointers

The Fast & Slow Pointers (Tortoise and Hare) pattern is used to detect cycles in linked lists and other similar structures.
Sample Problem:
Detect if a linked list has a cycle.
Explanation:
Initialize two pointers, one moving one step at a time (slow) and the other moving two steps at a time (fast).
If there is a cycle, the fast pointer will eventually meet the slow pointer.
If the fast pointer reaches the end of the list, there is no cycle.
LeetCode Problems:
5. LinkedList In-place Reversal

The In-place Reversal of a LinkedList pattern reverses parts of a linked list without using extra space.
Use this pattern when you need to reverse sections of a linked list.
Sample Problem:
Reverse a sublist of a linked list from position m to n.
Example:
Input:
head = [1, 2, 3, 4, 5],m = 2,n = 4Output:
[1, 4, 3, 2, 5]
Explanation:
Identify the start and end of the sublist.
Reverse the nodes in place by adjusting the pointers.
LeetCode Problems:
6. Monotonic Stack

The Monotonic Stack pattern uses a stack to maintain a sequence of elements in a specific order (increasing or decreasing).
Use this pattern for problems that require finding the next greater or smaller element.
Sample Problem:
Find the next greater element for each element in an array. Output -1 if the greater element doesn’t exist.
Example:
Input:
nums = [2, 1, 2, 4, 3]Output:
[4, 2, 4, -1, -1]
Explanation:
Use a stack to keep track of elements for which we haven't found the next greater element yet.
Iterate through the array, and for each element, pop elements from the stack until you find a greater element.
If the stack is not empty, set the result for index at the top of the stack to current element.
Push the current element onto the stack.
LeetCode Problems:
7. Top ‘K’ Elements

The Top 'K' Elements pattern finds the top k largest or smallest elements in an array or stream of data using heaps or sorting.
Sample Problem:
Find the k-th largest element in an unsorted array.
Example:
Input:
nums = [3, 2, 1, 5, 6, 4],k = 2Output:
5
Explanation:
Use a min-heap of size k to keep track of the k largest elements.
Iterate through the array, adding elements to the heap.
If the heap size exceeds k, remove the smallest element from the heap.
The root of the heap will be the k-th largest element.
LeetCode Problems:
8. Overlapping Intervals

The Overlapping Intervals pattern is used to merge or handle overlapping intervals in an array.
In an interval array sorted by start time, two intervals [a, b] and [c, d] overlap if b >= c (i.e., the end time of the first interval is greater than or equal to the start time of the second interval).
Sample Problem:
Problem Statement: Merge all overlapping intervals.
Example:
Input:
intervals = [[1, 3], [2, 6], [8, 10], [15, 18]]Output:
[[1, 6], [8, 10], [15, 18]]
Explanation:
Sort the intervals by their start time.
Create an empty list called
mergedto store the merged intervals.Iterate through the intervals and check if it overlaps with the last interval in the
mergedlist.If it overlaps, merge the intervals by updating the end time of the last interval in
merged.If it does not overlap, simply add the current interval to the
mergedlist.
LeetCode Problems:
9. Modified Binary Search

The Modified Binary Search pattern adapts binary search to solve a wider range of problems, such as finding elements in rotated sorted arrays.
Use this pattern for problems involving sorted or rotated arrays where you need to find a specific element.
Sample Problem:
Find an element in a rotated sorted array.
Example:
Input:
nums = [4, 5, 6, 7, 0, 1, 2],target = 0Output:
4
Explanation:
Perform binary search with an additional check to determine which half of the array is sorted.
We then check if the target is within the range of the sorted half.
If it is, we search that half; otherwise, we search the other half.
LeetCode Problems:
10. Binary Tree Traversal

Binary Tree Traversal involves visiting all the nodes in a binary tree in a specific order.
PreOrder:
root -> left -> rightInOrder:
left -> root -> rightPostOrder:
left -> right -> root
Sample Problem:
Problem Statement: Perform inorder traversal of a binary tree.
Example:
Input:
root = [1, null, 2, 3]Output:
[1, 3, 2]
Explanation:
Inorder traversal visits nodes in the order: left, root, right.
Use recursion or a stack to traverse the tree in this order.
LeetCode Problems:
PreOrder → Binary Tree Paths (LeetCode #257)
PostOrder → Binary Tree Maximum Path Sum (LeetCode #124)
