My Ruby Solution, 88ms


  • 0
    W
      def remove_elements(head, val)
        return nil if head.nil?
        node = head
        while node.next != nil
          if node.next.val == val
            node.next = node.next.next
          else
            node = node.next
          end
        end
        if head.val == val
          head = head.next
        end
        head
      end

Log in to reply
 

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