203. Remove Linked List Elements

Source code notebook Author Update time

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
# @lc code=start
using LeetCode

function remove_elements!(
    head::Union{ListNode,Nothing}, value::Int
)::Union{ListNode,Nothing}
    cur = fake_head = ListNode()
    fake_head.next = head
    while !isnothing(cur.next)
        if cur.next.val == value
            cur.next = cur.next.next
        else
            cur = cur.next
        end
    end
    return fake_head.next
end
# @lc code=end
remove_elements! (generic function with 1 method)

This page was generated using DemoCards.jl and Literate.jl.