题解 | #删除链表的节点#
删除链表的节点
https://www.nowcoder.com/practice/f9f78ca89ad643c99701a7142bd59f5d
class Solution: def deleteNode(self , head: ListNode, val: int) -> ListNode: # write code here # 如果删除的是头结点 if head.val == val: return head.next # 设置两个结点:p1为删除结点p2的前一个结点 p1 = head p2 = head.next while p2.val != val and p2: p2 = p2.next p1 = p1.next else: p1.next = p2.next return head