Using ListNode


  • 0
    P
    class MinStack {
    
    	/** initialize your data structure here. */
    	boolean firstPush = true;
    
    	class ListNode {
    		ListNode next;
    		int val;
    
    		public ListNode(int num) {
    			val = num;
    			next = null;
    		}
    
    	}
    
    	ListNode head;
    	ListNode temp = new ListNode(-1);
    	ListNode minNode;
    
    	public MinStack() {
    
    	}
    
    	public void push(int x) {
    
    		ListNode temp1 = new ListNode(x);
    		temp1.next = head;
    		head = temp1;
    
    	}
    
    	public void pop() {
    		head = head.next;
    
    	}
    
    	public int top() {
    		return head.val;
    	}
    
    	public int getMin() {
    		minNode = head;
    		int min = 0;
    		min = minNode.val;
    		while (minNode != null) {
    			if (minNode.val <= min) {
    				min = minNode.val;
    			}
    			minNode = minNode.next;
    		}
    
    		return min;
    
    	}
    }

Log in to reply
 

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