为了处理头节点时能够统一,添加了虚拟头结点vHead
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):
def removeElements(self, head, val):
"""
:type head: ListNode
:type val: int
:rtype: ListNode
"""
vHead=ListNode(next=head)
current=vHead
while current.next:
if current.next.val==val:
current.next=current.next.next
else:
current=current.next
return vHead.next
知识点
python 中链表的定义
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
node = ListNode(5)