题目描述
给定单向链表的头指针和一个要删除的节点的值,定义一个函数删除该节点。
返回删除后的链表的头节点。
思路
代码
class Solution:
def deleteNode(self,head:ListNode,val:int)->ListNode:
if head.val == val:
return head.next
pre, cur = head, head.next
while cur and cur.val != val:
pre, cur = cur, cur.next
if cur and cur.val == val:
pre.next = cur.next
return head