Memory exceeded error using Linked List


  • 0
    S
    struct MinStackEntry{
        int val;
        int min;
        MinStackEntry* next;
        MinStackEntry(int x){
            val = x;
            next = NULL;
        }
    };
    
    class MinStack {
    private:
      MinStackEntry* head;
        
    public:
        MinStack(){
            head = NULL;
        }
    
        void push(int x) {
            MinStackEntry* temp = new MinStackEntry(x);
           
            if(!head){
                head = temp;
                head->min = x;
            }
            else{
                temp->min = min(temp->val, head->val);
                temp->next = head;
                head= temp;
            }
        }
    
        void pop() {
            if(!isEmpty()){
                MinStackEntry* temp = head;
                head = head->next;
                free(temp);
            }
        }
    
        int top() {
            return head->val;
        }
    
        int getMin() {
            return head->min;
        }
        
        bool isEmpty(){
            return !head;
        }
    };
    

    Why does my solution using a custom EntryElement which stores the minimum until now throw an memory exceeded error?


Log in to reply
 

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