Implement Stack using Linked List | Practice | GeeksforGeeks

Let's give it a try! You have a linked list and you have to implement the functionalities push and pop of stack using this given linked list. Your task is to use the class as shown in the comments in the code editor and complete the functions push() and pop() to implement a stack. 
Example 1:
Input: 
push(2)
push(3)
pop()
push(4) 
pop()
Output: 3 4
Explanation: 
push(2)    the stack will be {2}
push(3)    the stack will be {2 3}
pop()      poped element will be 3,
           the stack will be {2}
push(4)    the stack will be {2 4}
pop()      poped element will be 4
Example 2:
Input: 
pop()
push(4)
push(5)
pop()
Output: -1 5
Your Task: You are required to complete two methods push() and pop(). The push() method takes one argument, an integer 'x' to be pushed into the stack and pop() which returns an integer present at the top and popped out from the stack. If the stack is empty then return -1 from the pop() method.

Expected Time Complexity: O(1) for both push() and pop().
Expected Auxiliary Space: O(1) for both push() and pop().

Constraints:
1 <= Q <= 100
1 <= x <= 100
0
nityabhanu5 hours ago
EASY CPP SOLUTIONNote(Hint): The top node of stack is acting like the header node of a linked list 
CODE:
void MyStack ::push(int x) {  StackNode *s=new StackNode(x);  s->next=top;  top=s;}
//Function to remove an item from top of the stack.int MyStack ::pop() {  if(!top)  {      return -1;  }  int data=top->data;  top=top->next;  return data;}
0
madhukartemba2 weeks ago
JAVA SOLUTION:
class MyStack 
{
    StackNode top = null;
    
    //Function to push an integer into the stack.
    void push(int a) 
    {
        StackNode node = new StackNode(a);
        node.next = top;
        top = node;
    }
    
    //Function to remove an item from top of the stack.
    int pop() 
    {
        if(top==null) return -1;
        
        StackNode new_top = top.next;
        
        int data = top.data;
        
        //Garbage collector in Java will take care of it.
        top.next = null;
        top = null;
        
        top = new_top;
        
        return data;
        
    }
}
+1
nevilvas3 weeks ago
class Node:    
    def __init__(self,data):
        self.data = data
        self.next = None
        



class MyStack:
    def __init__(self):
        self.top = None
    #Function to push an integer into the stack.
    def push(self, data):
        temp = Node(data)
        temp.next = self.top
        self.top = temp

        


    #Function to remove an item from top of the stack.
    def pop(self):

        # Add code here
        if self.top == None:
            return -1
        ans = self.top.data
        self.top = self.top.next
        return ans
0
sangrambachu4 weeks ago
class MyStack 
{
    StackNode top;
    
    //Function to push an integer into the stack.
    void push(int a) 
    {
        // Add your code here
        StackNode temp = new StackNode(a);
        temp.next = top;
        top = temp;
    }
    
    //Function to remove an item from top of the stack.
    int pop() 
    {
        // Add your code here
        if(top == null) {
            return -1;
        }
        
        int a = top.data;
        top = top.next;
        return a;
    }
}
0
tarunkanade1 month ago
0.5/1.6
//Function to push an integer into the stack.
    void push(int a) 
    {
        // Add your code here
        StackNode temp = new StackNode(a);
        temp.next = top;
        top = temp;
    }
    
    //Function to remove an item from top of the stack.
    int pop() 
    {
        // Add your code here
        if(top == null){
            return -1;
        }
        
        int res = top.data;
        
        top = top.next;
        
        return res;
    }
0
swastikp17111 month ago
Simple Stack Implementation Using LinkedList in Java 
class MyStack 
{
    StackNode top;
    
    MyStack(){
        top=null;
    }
    //Function to push an integer into the stack.
    void push(int a) 
    {
        StackNode newNode=new StackNode(a);
        newNode.next=top;
        top=newNode;
    }
    
    //Function to remove an item from top of the stack.
    int pop() 
    {
    	// condition for Empty Stack
        if(top==null) return -1;
        
        // When Stack is not Empty
        int ans=top.data;
        top=top.next;
        
        return ans;
    }
}
 
0
atif836141 month ago
main opreation in stack :
// this is push opreation:
void push(int a){
StackNode newnode=new StackNode(a);
newnode.next=top;
top = newnode;
}
//    pop opreation :
int pop(){
int tempdata;
if(top==null){
return -1;
}
else
tempdata=top.data;
top = top.next;
return tempdata;
}
 
0
akasharma9121 month ago
void MyStack ::push(int x) {   StackNode * t = new StackNode(x);   t->next=top;   top=t;}
//Function to remove an item from top of the stack.int MyStack ::pop() {   if (top==NULL)   {       return -1;   }   else{       int element =top->data;       top=top->next;       return element;   }} 
0
yashkotalwar101 month ago
// Java
void push(int a)    {       StackNode stackNode = new StackNode(a);       stackNode.next = top;       top = stackNode;   }   int pop()    {       if (top == null){           return -1;       }       int temp = top.data;       top = top.next;       return temp;   }
0
sayantankuila1 month ago
C solution
void push(struct Stack* stack, int item){   stack->top++;   stack->array[stack->top] = item;   }
// Function to remove an item from stack.  It decreases top by 1int pop(struct Stack* stack){   if(stack->top == -1){       return -1;   }      else{       int v= stack->array[stack->top];       stack->top--;       return v;       }   }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.