203. Remove Linked List Elements
Remove all elements from a linked list of integers that have value val.
Example:
Input: 1->2->6->3->4->5->6, val = 6
Output: 1->2->3->4->5
def remove_elements(head, val)
return nil if head.nil?
node = head
while node.next != nil
if node.next.val == val
node.next = node.next.next
else
node = node.next
end
end
if head.val == val
head = head.next
end
head
end
Last updated
Was this helpful?