classSolution: defdeleteNode(self , head: ListNode, val: int) -> ListNode: #加入一个头节点 res = ListNode(0) res.next = head #前序节点 pre = res #当前节点 cur = head #遍历链表 while cur isnotNone: #找到目标节点 if cur.val == val: #断开连接 pre.next = cur.next break pre = cur cur = cur.next #返回去掉头节点 return res.next