Python LinkedList Solution


  • 1

    Linked list is useful for deleting elements.

    class HitCounter(object):
    
        def __init__(self):
            self.head = ListNode(0)
            self.c = self.head
            self.count = 0
    
        def hit(self, timestamp):
            self.c.next = ListNode(timestamp)
            self.c = self.c.next
            self.count += 1
            self.clean(timestamp)
            
        def getHits(self, timestamp):
            self.clean(timestamp)
            return self.count
        
        def clean(self, timestamp):
            bound = timestamp - 300
            h = self.head
            while h.next and h.next.val <= bound:
                h = h.next
                self.count -= 1
            self.head = h

Log in to reply
 

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