remove-linked-list-elements

This commit is contained in:
Gleb Koval 2021-11-12 09:41:28 +00:00
parent dbe344221b
commit 47b81f8c68
No known key found for this signature in database
GPG Key ID: 7C89CDC822F8392B
1 changed files with 19 additions and 0 deletions

View File

@ -0,0 +1,19 @@
package main
type ListNode struct {
Val int
Next *ListNode
}
func removeElements(head *ListNode, val int) *ListNode {
top := &ListNode{Val: 0, Next: head}
last := top
for current := head; current != nil; current = current.Next {
if current.Val == val {
last.Next = current.Next
} else {
last = current
}
}
return top.Next
}