package main
type ListNode struct{
Val int
Next *ListNode
}funcdeleteNode(head *ListNode, val int)*ListNode {if head.Val == val {return head.Next
}var pre = head
var cur = head.Next
for; cur !=nil&& cur.Val != val;{
pre = pre.Next
cur = cur.Next
}if cur !=nil{
pre.Next = cur.Next
}return head
}
Python
classListNode:def__init__(self, x):
self.val = x
self.next=NoneclassSolution:defdeleteNode(self, head: ListNode, val:int)-> ListNode:if head.val == val:return head.next
pre, cur = head, head.nextwhile cur and cur.val != val:
pre, cur = pre.next, cur.nextif cur: pre.next= cur.nextreturn head
C#
publicclassListNode{publicint val;publicListNode next;publicListNode(int x){ val = x;}}publicclassSolution{publicListNodeDeleteNode(ListNode head,int val){if(head.val == val){return head.next;}ListNode pre = head;ListNode cur = head.next;while(cur !=null&& cur.val != val){
pre = pre.next;
cur = cur.next;}if(cur !=null){
pre.next = cur.next;}return head;}}