203. Remove Linked List Elements
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
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func removeElements(head *ListNode, val int) *ListNode {
if head == nil {
return nil
}
dummy := ListNode{Val: 0}
node := head
preNode := &dummy
for node != nil {
if node.Val != val {
preNode.Next = node
preNode = node
}
node = node.Next
}
preNode.Next=node
return dummy.Next
}