306. Drop the Value
A librarian keeps a chain of index cards, each card carrying a catalogue number and a pointer to the next card. A particular number v has been withdrawn, and every card carrying that number must be thrown out of the chain wherever it appears, including at the very front.
Given the head of the chain and the integer v, remove every node whose value equals v and return the head of the remaining chain. The relative order of the surviving cards must not change. If every card is removed, the result is an empty chain. The chain can hold up to 100,000 cards.
Example 1
- Input:
- head = [7,2,7,7,9,2,4], v = 7
- Output:
- [2,9,2,4]
- Explanation:
The three nodes holding 7 disappear, leaving 2, 9, 2, 4.
Example 2
- Input:
- head = [6,6,6], v = 6
- Output:
- []
- Explanation:
Every card matches, so nothing is left.
Constraints
0 ≤ chain length ≤ 105
-1000 ≤ node value, v ≤ 1000
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Expected complexity
- Time
- O(n)
- Space
- O(1)