题解 | #删除链表的节点#
https://www.nowcoder.com/practice/f9f78ca89ad643c99701a7142bd59f5d
概述
首先,申请虚节点,因为要是删除头节点,没有虚拟节点不行
其次,其实只需要使用一个节点 cur 就可以了,
class Solution: def deleteNode(self , head: ListNode, val: int) -> ListNode: # write code here dummy = ListNode(None) dummy.next = head cur = dummy while cur.next: if cur.next.val == val: cur.next = cur.next.next cur = cur.next return dummy.next