Remove Linked List Elements
Solution in go
func removeElements(head *ListNode, val int) *ListNode {
    var (
        prev *ListNode = nil
        curr *ListNode = head
    )

    for curr != nil {
        if curr.Val == val {
            // Head node
            if prev == nil {
                head = head.Next
                curr = head
            } else {
                prev.Next = curr.Next
                curr = curr.Next
            }
            continue
        }
        prev = curr
        curr = curr.Next
    }

    return head
}
    TheDeveloperCafe © 2022-2024