20 lines
346 B
Go
20 lines
346 B
Go
|
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
|
||
|
}
|