Linked lists
Pointer surgery. A sentinel head removes most of the edge cases.
Trigger in the prompt: 'Linked list', 'reverse', 'from the end', 'cycle'.
easyMerge Two Sorted Lists
Merge two sorted linked lists into one sorted list.
Trigger'Merge two sorted things'.
Approach- Use a sentinel (dummy) head — it removes every empty-list and first-node special case.
- Walk both lists, always attaching the smaller head.
- At the end, attach whichever list still has nodes; there is no need to walk it.
O(n + m) time, O(1) space.
PitfallNot using a sentinel and then writing three branches of special-case code for the first node.
Say it out loud“I'll use a dummy head node — it costs one allocation and removes every edge case around the first node and empty inputs. Then it's a single walk, and at the end I attach the remainder wholesale rather than copying it node by node.”
Solve on LeetCodeeasyReverse Linked List
Reverse a singly linked list.
TriggerThe most basic pointer manipulation there is.
Approach- Three pointers: previous, current, next. Save next before rewriting current's link.
- Return previous, not current — current is null at the end.
- Know the recursive version too; the interviewer often asks for both.
O(n) time, O(1) space iteratively; O(n) stack recursively.
PitfallLosing the rest of the list by rewriting `current.next` before saving it.
Say it out loud“Three pointers, and the ordering matters: I save the next node before I rewrite the current one's link, otherwise I lose the rest of the list. Iterative is constant space; the recursive version is elegant but costs a stack frame per node.”
Solve on LeetCodehardReverse Nodes in k-Group
Reverse the list in consecutive groups of k, leaving any remainder untouched.
Trigger'In groups of k' plus 'leave the tail alone'.
Approach- Check there are k nodes remaining **before** reversing the group.
- Reverse the group with the standard three-pointer loop, bounded to k steps.
- Reconnect: the previous group's tail points at the new head; the new tail points at the following group.
- A sentinel head makes the first group uniform with the rest.
O(n) time, O(1) space.
PitfallReversing a final partial group. Check the count first — that check is the specification.
Say it out loud“The reversal itself I know; the work here is bookkeeping. I check that k nodes remain before touching anything, reverse exactly k, and then reconnect three pointers. A dummy head means the first group isn't a special case.”
Solve on LeetCode