3 Sum
Sort the array, fix one value at a time, and use two pointers on the remaining range. Skip duplicate anchors and duplicate pointer values so the result contains unique triplets only.
Return all unique triplets in an array whose sum is zero.
Tagged with two-pointers
Sort the array, fix one value at a time, and use two pointers on the remaining range. Skip duplicate anchors and duplicate pointer values so the result contains unique triplets only.
Return all unique triplets in an array whose sum is zero.
Use left and right pointers, compute the current area, and always move the side with the smaller height because only that can improve the limiting wall.
Find two lines that together hold the maximum area of water.
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.
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.
Walk both arrays with two pointers, append the smaller value each step, and then append the remaining tail from whichever array is not exhausted yet.
Merge two sorted arrays into one sorted list.
Use a dummy head and repeatedly attach the smaller current node from either list. When one list runs out, append the remaining nodes from the other list.
Merge two sorted linked lists and return the head of the merged list.
Scan once, swap each non-zero value into the next write position, and advance that write pointer.
Move all zeroes to the end of the list while keeping the relative order of non-zero values.
Compare characters from the left and right ends while moving inward. Normalize case or strip non-alphanumeric characters only if the prompt requires it.
Determine whether a string reads the same forward and backward.
Use a dummy node, move the fast pointer n steps ahead, then move both fast and slow together until fast reaches the end. The slow pointer will be just before the node to remove.
Remove the nth node from the end of a linked list and return the head.
Start with one pointer at the left and one at the right. Move the left pointer rightward when the sum is too small and the right pointer leftward when the sum is too large.
Given a sorted array, return the indices of two numbers that add up to a target.