mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-04 08:02:30 +08:00
34 lines
543 B
Go
34 lines
543 B
Go
package leetcode
|
|
|
|
import (
|
|
"github.com/halfrost/LeetCode-Go/structures"
|
|
)
|
|
|
|
// ListNode define
|
|
type ListNode = structures.ListNode
|
|
|
|
/**
|
|
* Definition for singly-linked list.
|
|
* type ListNode struct {
|
|
* Val int
|
|
* Next *ListNode
|
|
* }
|
|
*/
|
|
func removeElements(head *ListNode, val int) *ListNode {
|
|
if head == nil {
|
|
return head
|
|
}
|
|
newHead := &ListNode{Val: 0, Next: head}
|
|
pre := newHead
|
|
cur := head
|
|
for cur != nil {
|
|
if cur.Val == val {
|
|
pre.Next = cur.Next
|
|
} else {
|
|
pre = cur
|
|
}
|
|
cur = cur.Next
|
|
}
|
|
return newHead.Next
|
|
}
|