Where is the wrong in my code?


  • 0
    J

    class MinStack {
    public void push(int x) {
    Node node=new Node();
    node.value=x;
    if(this.min==null)
    this.min=node;
    else if(node.value<this.min.value)
    {
    node.lastMin=this.min;
    this.min=node;
    }
    node.next=this.head;
    this.head=node;

    }
    
    public void pop() {
    	Node node=this.head ;
        if(this.head!=null)
        {
        	if(this.head.lastMin!=null)
        		this.min=this.head.lastMin;
        	this.head =node.next;
        node=null;
        System.gc();}
        else return;
        
    }
    
    public int top() {
        if(this.head==null)
        	return 0;
        else
        	return this.head.value;
    }
    
    public int getMin() {
        if(this.min!=null)
        	return this.min.value;
        else
        	return -1;
    }
    class Node{
    	int value;
    	Node next;
    	Node lastMin;
    	Node(){
    		next=null;
    		lastMin=null;
    	}
    }
    
    	Node head;
    	Node min;
    	MinStack(){
    		head=null;
    		min=null;
    	}
    

    }


  • 0
    S
    This post is deleted!

Log in to reply
 

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