# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def removeElements(self, head, val):
"""
:type head: ListNode
:type val: int
:rtype: ListNode
"""
dummy=cur=ListNode(0)
dummy.next=head
while cur and cur.next:
if cur.next.val==val:
cur.next=cur.next.next
else:
cur=cur.next
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def removeElements(self, head, val):
"""
:type head: ListNode
:type val: int
:rtype: ListNode
"""
dummy=cur=ListNode(0)
dummy.next=head
while cur and cur.next:
if cur.next.val==val:
cur.next=cur.next.next
else:
cur=cur.next
return dummy.next
学习视频https://www.youtube.com/watch?v=l3fNiN1Fz9E