Detect Cycle in Linked List
Move one pointer one step and another pointer two steps at a time. If they ever meet, there is a cycle; if the fast pointer reaches the end, the list is acyclic.
Return whether a linked list contains a cycle.
Quick recall
Move one pointer one step and another pointer two steps at a time. If they ever meet, there is a cycle; if the fast pointer reaches the end, the list is acyclic.
Return whether a linked list contains a cycle.
Track only the previous two numbers and iterate until you reach n. This keeps the solution clear and avoids exponential recursion.
Return the nth Fibonacci number.
Walk the list once, keep a set of seen values, and add repeated values into a duplicate set. Convert to a sorted list if stable output helps.
Return the duplicate values from a list of integers.
Advance a slow pointer by one step and a fast pointer by two steps. When the fast pointer reaches the end, the slow pointer will be at the middle.
Return the middle node of a singly linked list.
Build a frequency map for the characters, then iterate through the string a second time to return the first character whose count is one.
Return the first character in a string that appears exactly once, or an empty string if none exists.
Use a dictionary keyed by the sorted characters of each word, then append each word into the matching bucket.
Group words that are anagrams of each other and return the grouped lists.
Track the best result when including or excluding the current house. A rolling two-variable DP solution keeps the implementation compact.
Return the maximum amount you can rob from houses without robbing adjacent houses.
Wrap a `deque` inside a small class so enqueue uses `append`, dequeue uses `popleft`, and peek reads the leftmost item without removing it.
Implement a queue with enqueue, dequeue, and peek operations.
Convert both arrays to sets, intersect them, and sort the result if you want deterministic output for testing or interviews.
Return the unique values that appear in both arrays.
Push each number into a heap, trim the heap whenever it grows beyond size k, and the root will end up as the kth largest value.
Return the kth largest element from an unsorted list.
Shrink the candidate prefix until every string starts with it. If it becomes empty, there is no shared prefix.
Return the longest common prefix shared by all strings in a list.
Put all numbers in a set, then expand sequences only from values whose previous number is absent. That avoids re-counting the same streak multiple times.
Return the length of the longest consecutive sequence in an unsorted array.