Remove nodes with a given value
Company: Capital One
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates proficiency in singly linked list manipulation, specifically pointer handling and in-place node removal, and falls under the Coding & Algorithms domain.
Constraints
- 0 <= len(head) <= 10^5
- Each node value and target fits in a 32-bit signed integer
- The intended linked list algorithm should run in O(n) time
- Use O(1) auxiliary space for the linked list pointer manipulation
Examples
Input: ([1, 2, 6, 3, 4, 5, 6], 6)
Expected Output: [1, 2, 3, 4, 5]
Explanation: Both nodes with value 6 are removed, including the one at the tail.
Input: ([7, 7, 7], 7)
Expected Output: []
Explanation: Every node matches the target, so the result is an empty list.
Input: ([], 1)
Expected Output: []
Explanation: The input list is already empty, so nothing changes.
Input: ([5], 5)
Expected Output: []
Explanation: The single node matches the target and is removed.
Input: ([-1, 2, -1, 3, -1], -1)
Expected Output: [2, 3]
Explanation: All occurrences of -1 are removed, leaving only 2 and 3.
Input: ([2147483647, -2147483648, 2147483647], 2147483647)
Expected Output: [-2147483648]
Explanation: Both maximum 32-bit integer values are removed, leaving the middle node.
Hints
- A dummy (sentinel) node before the head makes it easy to delete nodes at the front of the list.
- Instead of moving two pointers separately, try checking current.next so you can relink around nodes that should be removed.