Why is my solution wrong? Can anybody explain it to me? Even if it is different from other solutions.


  • -1
    K

    class MinStack{

    // link list format stack
    private class Node{
        int item;
        Node next;
    }
    private Node first;
    
    public void push(int x) {
        Node oldfirst = first;
        first = new Node();
        first.item = x;
        first.next = oldfirst;
    }
    
    public void pop() {
        if(first == null){
            return null;
        }else{
        int item = first.item;
        first = first.next;
        return item;
        }
    }
    
    public int top() {
        int item = first.item;
        return item;
    }
    
    public int getMin() {
        if(first == null){
            return null;
        }
        else{
        int min;
        Node currentNode = first;
        min = first.item;
        Node nextNode = first.next;
        while( nextNode!= null){
            if(min > nextNode.item){
                min = nextNode.item;
            }
            currentNode = nextNode;
            nextNode = nextNode.next;
        }
        return min;
        }
    }
    

    }

    the error is:
    Line 19: error: cannot return a value from method whose result type is void


  • 1
    M

    pop does not return a value. It has return type void.


Log in to reply
 

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