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
  1. Use a sentinel (dummy) head — it removes every empty-list and first-node special case.
  2. Walk both lists, always attaching the smaller head.
  3. At the end, attach whichever list still has nodes; there is no need to walk it.
Target complexity

O(n + m) time, O(1) space.

Pitfall

Not 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 LeetCode
easyReverse Linked List

Reverse a singly linked list.

Trigger

The most basic pointer manipulation there is.

Approach
  1. Three pointers: previous, current, next. Save next before rewriting current's link.
  2. Return previous, not current — current is null at the end.
  3. Know the recursive version too; the interviewer often asks for both.
Target complexity

O(n) time, O(1) space iteratively; O(n) stack recursively.

Pitfall

Losing 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 LeetCode
hardReverse 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
  1. Check there are k nodes remaining **before** reversing the group.
  2. Reverse the group with the standard three-pointer loop, bounded to k steps.
  3. Reconnect: the previous group's tail points at the new head; the new tail points at the following group.
  4. A sentinel head makes the first group uniform with the rest.
Target complexity

O(n) time, O(1) space.

Pitfall

Reversing 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