1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// @Title: 删除链表的节点 (删除链表的节点 LCOF)
// @Author: 15816537946@163.com
// @Date: 2022-02-13 15:36:32
// @Runtime: 0 ms
// @Memory: 2.7 MB
/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func deleteNode(head *ListNode, val int) *ListNode {
    if head == nil || head.Next == nil {
        return head
    }
    if head.Val == val {
        head = head.Next
    }

    pre,cursor := head,head.Next
    for cursor != nil {
        if cursor.Val == val {
            pre.Next = cursor.Next
        }
        pre = cursor
        cursor = cursor.Next
    }

    return head
}