赞
踩
用了创建虚拟头结点的方法,比较简单
- class Solution:
- def removeElements(self, head: ListNode, val: int) -> ListNode:
- dummy_head = ListNode(next = head)
- cur = dummy_head
- while (cur.next != None):
- if(cur.next.val == val):
- cur.next = cur.next.next
- else:
- cur = cur.next
- return dummy_head.next
-
- emp1 = solution1()
- head=[1,2,6,3,4,5,6]
- val = 6
- emp1.removeElements(head, val)
- #输出:[1,2,3,4,5]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。