Remove linked list elements

September 9, 2022

linked-list

Problem URL: Remove linked list elements

We will create a dummy node and attach it before our head. Then we will iterate over each node, if the value of the node is equal to our node, we will remove them. Then we will return the next node of our dummy node as result.

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next

class Solution:
    def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]:
        if not head:
            return head

        dummy = ListNode(-1, head)
        prev, cur = dummy, head
        while cur:
            if cur.val == val:
                prev.next = cur.next
            else:
                prev = cur
            cur = cur.next

        return dummy.next

Time Complexity: O(n)
Space Complexity: O(1)