Two Pointers
two-pointers
Algorithms
Linked List Algorithms
Reversing a singly linked list is a four-line iteration with three pointers (`prev`, `curr`, `next`), and yet roughly a third of candidates implement it incorrectly under interview pressure. The reason is that linked-list problems give you no random access: every operation has to be expressed as careful, ordered pointer rewrites, and one missed assignment loses the rest of the list forever. **Linked List Algorithms** is the lesson where pointer manipulation becomes a reliable skill. You will reverse a list iteratively and recursively, then extend the technique to reverse in groups of `k`. You will use Floyd's fast and slow pointers to detect cycles, find the cycle start, locate the middle element, and check whether a list is a palindrome. You will merge two sorted lists, then merge `k` sorted lists with a min-heap. The lesson also covers removing the nth node from the end, finding the intersection of two lists, deep-copying a list with random pointers, and the dummy-node trick that eliminates head-edge cases entirely. In **Linked Lists (Singly)**, you saw how nodes and `next` pointers form a list and why insertion at a known position is `O(1)`. **Two Pointers (Intro)** introduced fast and slow indices on arrays; this lesson reuses the same idea on pointer-linked nodes. Next, **Tree Algorithms** generalizes pointer-and-recursion thinking to branching structures.
Not Started
0%
Two Pointers (Intro)
Given a sorted array and a target sum, the brute-force "check every pair" approach is `O(n^2)`. With two indices walking inward from opposite ends, the same problem drops to `O(n)`: if the current pair sums too low, move the left pointer right; if it sums too high, move the right pointer left. **Two Pointers (Intro)** turns that insight into a reusable pattern with two main shapes. The opposite-direction variant has pointers converge from both ends and powers problems like pair sum, palindrome check, reverse in place, and container with most water. The same-direction (fast and slow) variant has both pointers walk forward at different speeds and powers in-place filtering problems like remove duplicates from a sorted array, move zeros to the end, and the linked-list cycle detection you will revisit later. In **Arrays & Strings**, you learned how a single index walks across an array. This lesson coordinates two indices in a way that exploits sorted order or a structural invariant to skip work an inner loop would otherwise repeat. Next, **Sliding Window (Intro)** generalizes the same-direction pointer pair into an expanding and shrinking range that solves contiguous-subarray problems.
Not Started
0%
Practice Problems
Is Subsequence
Determine if string s is a subsequence of string t by checking if all characters of s appear in t in the same order.
Merge Sorted Array
Merge two sorted arrays into one sorted array in-place, using the extra space at the end of the first array.
Merge Two Sorted Lists
Merge two sorted linked lists into one sorted linked list by splicing the nodes together.
Remove Duplicates from Sorted Array
Remove duplicates from a sorted array in-place so each element appears only once, and return the new length.
Remove Element
Remove all occurrences of a given value from an array in-place and return the new length.
Valid Palindrome
Determine if a string is a palindrome, considering only alphanumeric characters and ignoring cases.
Trapping Rain Water
Given an elevation map, compute how much water it can trap after raining.
Container With Most Water
Find two lines that together with the x-axis form a container holding the most water.
Partition List
Given the head of a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x, preserving the original relative order.
Remove Duplicates from Sorted Array II
Remove duplicates from a sorted array in-place so each element appears at most twice, using a two-pointer technique.
Remove Nth Node From End of List
Given the head of a linked list, remove the nth node from the end of the list and return its head.
Reorder List
Reorder a linked list from L0 -> L1 -> ... -> Ln to L0 -> Ln -> L1 -> Ln-1 -> L2 -> Ln-2 -> ... in-place.
Reverse Words in a String
Reverse the order of words in a string, handling leading/trailing spaces and multiple spaces between words.
Sort Colors
Sort an array containing only 0s, 1s, and 2s in-place using a single pass (Dutch National Flag problem).
3Sum
Find all unique triplets in an array that sum to zero, avoiding duplicate triplets in the result.
Two Sum II - Input Array Is Sorted
Given a 1-indexed sorted array, find two numbers that add up to a target and return their indices.
Code Snippets
Binary Search Template
Binary search is the most asked algorithm in interviews and the easiest to get wrong: off-by-one bugs, infinite loops, integer overflow on the midpoint. This snippet covers the iterative bounds template that always terminates, a recursive variant for contrast, and a found-or-insertion-point version that returns where the value would go if absent. The same skeleton powers the lower-bound and upper-bound variants in the next entry.
Two-Pointer Template
The two-pointer technique is the linear-time answer to many sorted-array problems: pair sums, palindrome checks, in-place reversal, partition. This snippet covers the opposite-ends sweep for sorted-pair targets, the reverse-in-place pattern, and the slow-fast pointer used for in-place mutation. Each fits in 10 lines and runs in O(n) time, O(1) extra space.
Sliding Window Template
Sliding window is the linear-time alternative to nested loops for substring-and-subarray problems. This snippet covers the fixed-size window for max-sum-of-k, the variable-size window for longest-substring-with-condition, and the at-most-K shrinkable form that handles 'at most / exactly K distinct' problems with one trick.
Lower-Bound and Upper-Bound Binary Search
Lower-bound and upper-bound binary search are the two primitives every other range query depends on. Lower-bound returns the first index where a value could be inserted; upper-bound returns the first index strictly greater than the value. This snippet covers both forms, then composes them to count occurrences in a sorted array in O(log n).
Python Two-Pointer Template
The two-pointer pattern walks two indices through a sorted array, moving them inward (or together) based on a comparison. It turns many naive O(n^2) problems into O(n) sweeps. This entry covers the inward-sweep template (two-sum sorted), the same-direction template (remove duplicates in place), and a 3-sum builder that uses two pointers as the inner loop.
Question Banks
Two-Pointer Warm-Up
Four short prompts covering pair sums, in-place dedupe, and palindrome checks with two pointers. Great preparation before sliding-window drills.
String Anagram and Palindrome
Five prompts on anagram detection by frequency and palindrome checks via two pointers, with one bug hunt and one canonical implementation.
Community
Sliding Window Technique
What sliding window actually optimises (state reuse, not just same-direction pointers), the fixed vs variable templates, four canonical problems, and when the technique is the wrong tool.
Boats to Save People
Given person weights, a boat capacity, and a 2-person-per-boat limit, find the minimum number of boats.
Odd Even Linked List
Reorder a singly-linked list so all odd-positioned nodes come before even-positioned ones, in O(n) time and O(1) space using two interleaved sub-lists.
Reverse Words in a String III
Reverse the characters of every word in a sentence while keeping the word order intact.
Subarray Product Less Than K
Count contiguous subarrays whose product is strictly less than k, in linear time using a sliding window.
Intersection of Two Linked Lists
Find the node where two singly-linked lists merge using the two-pointer rendezvous trick that runs in O(m + n) time and O(1) space.
Two Sum IV - Input is a BST
Decide whether two distinct BST nodes sum to a target, ideally in O(n) time and O(h) extra space.
Palindrome Linked List
Decide whether a singly-linked list reads the same forward and backward in O(n) time and O(1) space using fast/slow pointers plus an in-place reverse of the second half.
Move Zeroes
Move every zero to the end of the array in place while keeping the relative order of the non-zero elements.
Backspace String Compare
Compare two strings after applying backspace edits, with the catch that the O(1) space version uses two pointers walking from the end.
Find All Duplicates in an Array
Identify every value that appears twice in an array of integers in [1, n], using O(1) extra space via index-negation.
Squares of a Sorted Array
Return the squares of a sorted integer array, also sorted, in O(n) time using a two-pointer merge from the ends.
Find K Closest Elements
Find a length-k window in a sorted array whose elements are closest to x, using a binary search on the LEFT edge of the window.
Reverse String
Reverse a character array in place using two pointers, no extra buffer allowed.
Reverse Vowels of a String
Reverse only the vowels of a string while keeping consonants and punctuation in place.
Wildcard Matching
Match a string against a wildcard pattern with `?` (any single char) and `*` (any sequence) using O(m*n) 2D DP.
Sort Array By Parity
Rearrange the array so that every even integer appears before every odd integer; any valid arrangement is accepted.
Two Pointers Technique
The three sub-patterns (opposite ends, same direction, fast-slow), the canonical problems each one solves, and why recognizing the shape is the entire skill.
