RemoveKFromList
Given a singly linked list of integers l
and an integer k
, remove all elements from list l
that have a value equal to k
.
Example
For
l = [3, 1, 2, 3, 4, 5]
andk = 3
, the output should besolution(l, k) = [1, 2, 4, 5]
;For
l = [1, 2, 3, 4, 5, 6, 7]
andk = 10
, the output should besolution(l, k) = [1, 2, 3, 4, 5, 6, 7]
.
Idea
Iterate from the beginning and eliminate leading k
Iterate the mid-part and eliminate any single k or contiguous k
Code
Last updated