I jumped straight to the two-pointer approach without pausing to think about the head-removal edge case, which bit me.
Start by clarifying the problem and edge cases, then propose the dummy-head technique to simplify deletion at the head. Walk through the iterative traversal and pointer manipulation, and finally analyze time and space complexity, emphasizing how the dummy node avoids special-case handling.
Pro tip: Mention that the dummy-head approach is a common pattern for linked list problems involving head deletion or insertion, and that it can be extended to other problems like removing duplicates or partitioning lists.
Confirm the problem details: singly linked list, remove all nodes with value equal to target, return new head. Ask about edge cases like empty list, all nodes equal to target, or target not present.
Explain that a dummy node pointing to the original head simplifies deletion at the head by ensuring every node has a predecessor. This avoids separate logic for removing the head.
Use a pointer (prev) starting at dummy and another (curr) at head. While curr is not null, if curr.val equals target, set prev.next = curr.next and move curr; else, move prev to curr and curr to curr.next.
After traversal, the new head is dummy.next. Return it. Note that if all nodes are removed, dummy.next will be null, correctly returning an empty list.
Time complexity is O(n) since each node is visited once. Space complexity is O(1) because only a few pointers are used, regardless of list size.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.