Remove Linked List Elements
Problem Given the head of a singly linked list and an integer val, remove every node whose value equals val and return the new head.
Input / Output
- Input: head of a singly linked list, int val.
- Output: the new head after all matching nodes are removed.
Constraints
- Up to 10^4 nodes.
- Solve in O(n) time and O(1) space.
Example
- 1→2→6→3→4→5→6, val = 6 → 1→2→3→4→5.
- 7→7→7, val = 7 → empty list.
asked …