/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/funcdeleteNode(head*ListNode,valint)*ListNode{dummy:=&ListNode{Next:head}prev,cur:=dummy,headforcur!=nil{ifcur.Val==val{prev.Next=cur.Nextbreak}prev=curcur=cur.Next}returndummy.Next}
解法二:递归
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/funcdeleteNode(head*ListNode,valint)*ListNode{ifnil==head{returnnil}ifhead.Val==val{returnhead.Next}head.Next=deleteNode(head.Next,val)returnhead}