题目
Remove all elements from a linked list of integers that have value val.
解题思路
一个dummy节点指向头,维护一个cur节点,每当他下一个节点为要删除的数时,删除下一个节点,直到当前结点为最后一个
代码
class Solution:
def removeElements(self, head: ListNode, val: int) -> ListNode:
dummy = ListNode(0)
dummy.next = head
cur = dummy
while cur.next:
if cur.next.val == val:
cur.next = cur.next.next
else:
cur = cur.next
return dummy.next