?删除链表中等于给定值 val 的所有节点。
原题请参考链接https://leetcode-cn.com/problems/remove-linked-list-elements/
class Solution:
def removeElements(self, head: ListNode, val: int) -> ListNode:
while head and head.val == val:
head = head.next
cur = head
while cur and cur.next:
if cur.next.val == val:
cur.next = cur.next.next
else:
cur = cur.next
return head
class Solution:
def removeElements(self, head: ListNode, val: int) -> ListNode:
dummy_node = ListNode(0)
tmp.next = head
cur = dummy_node
while head:
if head.val == val:
cur.next = head.next
head = head.next
else:
cur = cur.next
head = head.next
return dummy_node.next
原文:https://www.cnblogs.com/bladers/p/14413657.html