Thought I had this immediately and started coding without thinking through the head removal case.
Start by clarifying the problem constraints and edge cases, then propose an iterative solution using a dummy node to simplify head removal. Walk through the algorithm step-by-step, emphasizing pointer manipulation and termination conditions, and analyze time and space complexity.
Pro tip: Mention that using a dummy node elegantly handles cases where the head itself needs to be removed, avoiding special-case code. Also, discuss potential pitfalls like losing references to the next node when deleting, and how to avoid them.
Ask about input constraints (e.g., empty list, all nodes equal val, val not present) and whether the list can be modified in place. Confirm return type and any memory constraints.
Suggest using a dummy node pointing to the head, then iterate with a current pointer to skip nodes with value equal to val. This handles head removal uniformly.
Describe initializing dummy node, setting prev to dummy, and iterating while current is not null. If current.val equals val, set prev.next to current.next; else move prev to current. Finally, return dummy.next.
State time complexity O(n) and space O(1). Provide examples, including edge cases, and mentally test the code to ensure correctness.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.