Lecture 3: Exam prep
The exam has three problem-solving questions, probably one each on linked lists, stacks and queues. This page collects the classic linked-list problems (many are LeetCode staples) that reuse the operations from Lecture 3 - Linked List. It is extra practice, not from the slides.
Problem patterns
The exam asks you to write short pointer code, so these are the classic linked-list problems (most also appear on LeetCode). Each uses the same node type:
struct Node { int val; Node* next; };
| Problem | Core idea | Time / extra space |
|---|---|---|
| Traverse, count, search | walk with a local pointer | O(n) / O(1) |
| Reverse the list | flip each next, keep three pointers |
O(n) / O(1) |
| Find the middle | slow and fast pointers | O(n) / O(1) |
| Detect a cycle | slow and fast pointers meet | O(n) / O(1) |
| Merge two sorted lists | dummy node + tail, take the smaller | O(n+m) / O(1) |
| Remove the nth node from the end | two pointers, gap of n | O(n) / O(1) |
| Remove duplicates (sorted) | compare with next, skip equal |
O(n) / O(1) |
| Remove all nodes with a key | dummy node + prev |
O(n) / O(1) |
- Dummy node: a fake node placed before
head, so inserting or deleting at the front is no longer a special case, and you returndummy.next. - Slow/fast pointers:
fastmoves two steps whileslowmoves one, orfaststarts n steps ahead. Whenfastfinishes,slowis at the answer.
Traverse, count, search
int count(Node* head) {
int n = 0;
for (Node* p = head; p != nullptr; p = p->next) ++n;
return n;
}
Node* search(Node* head, int key) {
for (Node* p = head; p; p = p->next)
if (p->val == key) return p;
return nullptr; // not found
}
Reverse a list
Node* reverse(Node* head) {
Node* prev = nullptr;
while (head) {
Node* nxt = head->next; // 1. save the rest
head->next = prev; // 2. flip this link
prev = head; // 3. advance prev
head = nxt; // 4. advance head
}
return prev; // prev is the new head
}
1 → 2 → 3 becomes 3 → 2 → 1. Save next before flipping, or you lose the rest of the list.
Find the middle
Node* middle(Node* head) {
Node* slow = head;
Node* fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
}
return slow;
}
For 1 → 2 → 3 → 4 → 5 it returns 3. For an even length such as 1 → 2 → 3 → 4 it returns the second middle (3). The loop test checks fast before fast->next so it never dereferences nullptr.
Detect a cycle (Floyd)
bool hasCycle(Node* head) {
Node* slow = head;
Node* fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
if (slow == fast) return true; // they can only meet inside a cycle
}
return false; // fast hit nullptr: no cycle
}
If there is a cycle, fast gains one node per step on slow and must eventually land on it. If there is none, fast reaches nullptr.
Merge two sorted lists
Node* mergeSorted(Node* a, Node* b) {
Node dummy{0, nullptr};
Node* 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 ? a : b; // attach whatever is left
return dummy.next;
}
It relinks the existing nodes; nothing is copied. <= takes from a on ties, which keeps the merge stable (same idea as orderedInsert).
Remove the nth node from the end
Node* removeNthFromEnd(Node* head, int n) { // assumes 1 <= n <= length
Node dummy{0, head};
Node* fast = &dummy;
Node* slow = &dummy;
for (int i = 0; i <= n; ++i) // fast gets n + 1 steps ahead
fast = fast->next;
while (fast) {
fast = fast->next;
slow = slow->next;
}
Node* doomed = slow->next; // slow is the predecessor
slow->next = doomed->next;
delete doomed;
return dummy.next;
}
For 1 → 2 → 3 → 4 → 5 with n = 2, slow stops at 3 and node 4 is removed, giving 1 → 2 → 3 → 5. The dummy node makes removing the head (n equal to the length) work with no special case.
Remove duplicates from a sorted list
Node* dedupe(Node* head) {
Node* cur = head;
while (cur && cur->next) {
if (cur->val == cur->next->val) {
Node* dup = cur->next;
cur->next = dup->next; // skip it
delete dup;
} else {
cur = cur->next; // advance only when nothing was removed
}
}
return head;
}
1 → 1 → 2 → 3 → 3 becomes 1 → 2 → 3. Duplicates are adjacent because the list is sorted.
Remove all nodes with a key
Node* removeAll(Node* head, int key) {
Node dummy{0, head};
Node* prev = &dummy;
while (prev->next) {
if (prev->next->val == key) {
Node* doomed = prev->next;
prev->next = doomed->next;
delete doomed; // do NOT advance prev
} else {
prev = prev->next;
}
}
return dummy.next;
}
After deleting, stay on the same prev: the new prev->next hasn’t been checked yet. Advancing there would skip a node, and fail on input like 7 → 7 → 3.
Draw the list, write each pointer (prev, cur, slow, fast) under the node it points at, and redraw after every loop iteration. Check the empty list, a one-node list, and the head and tail cases before you finish.
Cheat sheet
- Dummy node removes the head special case; return
dummy.next. - Slow/fast finds the middle and detects cycles; check
fast && fast->next. - Reverse: save
next, flipnext, advanceprevandhead. - Merge: dummy + tail, take the smaller, attach the leftover.
- nth from the end:
fastgetsn + 1steps ahead, then both move. - After a delete, don’t advance
prev. - Always check: empty list, one node, head, tail.