Python iterative solution


  • 0
    S

    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 = head.next
        if not head:
            return head
        node = head
        while node.next:
            if node.next.val == val:
                node.next = node.next.next
            else:
                node = node.next
        return head

Log in to reply
 

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