Python iterative solution

  • 0

    First go identify the first element in the list whose value isn't val. After that go through the remaining elements and skip over the ones whose value are val.

     def removeElements(self, head, val):
        while head and head.val == val:
            head =
        if not head:
            return head
        node = head
            if == val:
                node =
        return head

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.