描述
删除链表中等于给定值 val 的所有节点。
样例
样例 1:
输入:head = 1->2->3->3->4->5->3->null, val = 3
输出:1->2->4->5->null
样例 2:
输入:head = 1->1->null, val = 1
输出:null
from lintcode import (
ListNode,
)
"""
Definition of ListNode:
class ListNode(object):
def __init__(self, val, next=None):
self.val = val
self.next = next
"""
class Solution:
"""
@param head: a ListNode
@param val: An integer
@return: a ListNode
"""
def remove_elements(self, head: ListNode, val: int) -> ListNode:
# write your code here
if head == None:
return head
dummy = ListNode(0)
dummy.next = head
pre = dummy
while head:
if head.val == val:
pre.next = head.next
head = pre
pre = head
head = head.next
return dummy.next