Why I get a Memory Limit Access error in MinStack problem


  • 1
    L

    Here is the code:

    class MinStack {
    stackNode top;
    public MinStack(){
    top = null;
    }

    public void push(int x){
    	stackNode node = new stackNode(x);
    	node.min = node;
    	if(top == null) top = node;
    	else{
    		node.next = top;
    		if(top.min.val < node.min.val){
    			node.min = top.min;
    		}
    		
    		top = node;
    	}
    }
    
    public stackNode pop(){
    	stackNode result = top;
    	if(top!=null) top = top.next;
    	return result;
    }
    
    public stackNode top(){
    	return top;
    }
    
    public stackNode getMin(){
    	if(top!=null) return top.min;
    	else return null;
    }
    
    class stackNode{
    	int val;
    	stackNode next;
    	stackNode min;
    	public stackNode(int k){
    		val = k;
    		next = null;
    		min = this;
    	}
    }
    

    }

    I maintain a min pointer to the minimum value under a node.
    It works fine on some simple test case in my Eclipse, but leetcode judging system always reports a memeory limit access error


Log in to reply
 

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