1
pre
cur
同时加,如果不等于target
直到相等则跳过
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def deleteNode(self, head: ListNode, val: int) -> ListNode:
if head.val == val:
return head.next
pre = head
cur = head.next
while cur and cur.val != val:
pre = cur
cur = cur.next
if cur:
pre.next = cur.next
return head