Two-Pointers
LeetCode 148: Sort List
·2 mins
LeetCode
Medium
Linked-List
Two-Pointers
Divide-and-Conquer
Sorting
LeetCode 148 note: split in half with fast and slow pointers, sort each half recursively and merge; O(n log n) with no extra array
LeetCode 142: Linked List Cycle II
·2 mins
LeetCode
Medium
Hash-Table
Linked-List
Two-Pointers
Floyds-Cycle-Finding-Algorithm
LeetCode 142 note: after fast and slow meet, send one back to head and step both one at a time; they meet again at the cycle entry
LeetCode 151: Reverse Words in a String
·1 min
LeetCode
Medium
Two-Pointers
String
LeetCode 151 note: split() with no argument treats runs of spaces as one separator and drops leading and trailing spaces; reverse and join with single spaces
LeetCode 189: Rotate Array
·2 mins
LeetCode
Medium
Array
Math
Two-Pointers
LeetCode 189 note: take k %= n, then reverse three times: the whole array, the first k, and the last n - k, with O(1) extra space
LeetCode 647: Palindromic Substrings
·2 mins
LeetCode
Medium
Two-Pointers
String
Dynamic-Programming
LeetCode 647 note: expand around centers, counting one palindromic substring for each successful expansion
LeetCode 287: Find the Duplicate Number
·2 mins
LeetCode
Medium
Array
Two-Pointers
Binary-Search
Bit-Manipulation
LeetCode 287 note: treat i -> nums[i] as a linked list; the duplicate is the cycle entry, found with Floyd’s algorithm in O(1) space without modifying the array
LeetCode 143: Reorder List
·2 mins
LeetCode
Medium
Linked-List
Two-Pointers
Stack
Recursion
LeetCode 143 note: find the middle, reverse the second half, and merge the two alternately, cutting the first half’s tail
LeetCode 42: Trapping Rain Water
·2 mins
LeetCode
Hard
Array
Two-Pointers
Dynamic-Programming
Stack
LeetCode 42 note: each cell holds min(tallest on the left, tallest on the right) - its own height; use prefix and suffix maxima or two pointers
LeetCode 167: Two Sum II - Input Array Is Sorted
·2 mins
LeetCode
Medium
Array
Two-Pointers
Binary-Search
LeetCode 167 note: sorted, so move two pointers inward, shifting right when the sum is too large and left when too small; indices are 1-based
LeetCode 86: Partition List
·2 mins
LeetCode
Medium
Linked-List
Two-Pointers
LeetCode 86 note: two dummies collect nodes below x and at least x, then join them, setting the second part’s tail to None
LeetCode 82: Remove Duplicates from Sorted List II
·2 mins
LeetCode
Medium
Linked-List
Two-Pointers
LeetCode 82 note: every duplicated value goes, the head included, so use a dummy; on a duplicate skip to the next different value and link prev.next past it
LeetCode 80: Remove Duplicates from Sorted Array II
·1 min
LeetCode
Medium
Array
Two-Pointers
LeetCode 80 note: keep each value at most twice by comparing with nums[k - 2], two slots back from the write position