This site uses one functional cookie to keep feature rollouts consistent for you. Nothing is set until you choose. See the privacy notice.
Phase 2: Structures
Draw the arrows, then rewire them in the right order.
Builds on Two Pointers.
The question to ask
Which of the four moves (dummy head, reverse, fast/slow, gap of n) does this need, and in what order?
Linked-list questions are pointer bookkeeping, and they repeat a handful of moves: a dummy head so the first node isn't a special case, reversing by walking prev/current/next, fast and slow pointers to find the middle or a cycle, and a gap of n between two pointers to find the nth from the end. Most medium list questions are two of those moves glued together.
Time O(n) · Space O(1) for the pointer moves
Write it from memory first, in an editor or on paper, then check. Recalling it is what makes it stick; reading it again mostly doesn't.
Fast moves two, slow moves one. If there's a cycle they must meet; if fast reaches null there isn't. Stop at fast.next being null instead and slow sits at the middle.
function hasCycle(head: ListNode | null): boolean {
let slow = head;
let fast = head;
while (fast && fast.next) {
slow = (slow as ListNode).next;
fast = fast.next.next;
if (slow === fast) return true;
}
return false;
}
function middleNode(head: ListNode | null): ListNode | null {
let slow = head;
let fast = head;
while (fast && fast.next) {
slow = (slow as ListNode).next;
fast = fast.next.next;
}
return slow;
}Merge Two Sorted Lists: a dummy node in front means the tail always has somewhere to attach, so there's no special case for the first node. Return dummy.next.
function mergeTwoLists(a: ListNode | null, b: ListNode | null): ListNode | null {
const dummy = new ListNode();
let tail = dummy;
while (a && b) {
if (a.val <= b.val) {
tail.next = a;
a = a.next;
} else {
tail.next = b;
b = b.next;
}
tail = tail.next;
}
tail.next = a ?? b;
return dummy.next;
}Easy to hard. Read the prompt and name the signal before opening the tell. After each attempt, rate it: Again brings it back tomorrow, Hard holds the interval, Good and Easy push it further out.
Given the head of a singly linked list, reverse the list and return the new head.
"Reverse" a "linked list": prev, current, next. Do it both iteratively and recursively.
Not attempted yet
Merge two sorted lists into one sorted linked list by splicing their nodes together, and return its head.
"Merge two sorted lists": a dummy head and a tail pointer that always takes the smaller node.
Not attempted yet
Given the head of a linked list, determine whether it has a cycle, using O(1) memory.
"Cycle" with "O(1) memory" rules out a visited Set. Fast and slow pointers.
Not attempted yet
Given the head of a singly linked list, return the middle node. With two middles, return the second.
"Middle" of a list in one pass: when fast reaches the end, slow is halfway.
Not attempted yet
Given the head of a singly linked list, return true if it is a palindrome, ideally in O(n) time and O(1) space.
Two moves glued together: find the middle with fast/slow, reverse the back half, then compare.
Not attempted yet
Remove the nth node from the end of the linked list and return its head, in one pass.
"Nth node from the end": move one pointer n ahead, then walk both until the leader hits the end. A dummy head handles removing the first node.
Not attempted yet
Reorder L0 → L1 → … → Ln into L0 → Ln → L1 → Ln-1 → …, changing the nodes rather than their values.
"Reorder" into first, last, second, second-last: find the middle, reverse the second half, weave the two together.
Not attempted yet
Two non-negative numbers are given as linked lists where the digits are stored in reverse order. Add them and return the sum as a linked list.
Reverse digit order means you add from the head: walk both lists with a carry, and keep going while either list or the carry remains.
Not attempted yet
Each node has a next pointer and a random pointer to any node or null. Return a deep copy of the list.
"Random pointer" plus deep copy: a Map from each original node to its copy, then wire next and random in a second pass.
Not attempted yet
Design a least recently used cache with get and put in O(1) average time, evicting the least recently used key when full.
"Least recently used" in O(1): a hash map to find nodes, a doubly linked list to move them to the front. (In JavaScript a Map's insertion order does it too: say both.)
Not attempted yet
An array of n + 1 integers holds values in 1..n, with one repeated. Find it without modifying the array and using constant extra space.
"Without modifying the array" and constant space: treat each value as a next pointer, so the duplicate is where a cycle starts. Floyd's algorithm.
Not attempted yet
Reverse the nodes of a linked list k at a time and return the head. A leftover group shorter than k stays as it is.
"k-group": check that k nodes remain, reverse exactly those, reconnect the group's ends, repeat. Dummy head throughout.
Not attempted yet