LinkedList pop() Method in Java

The java.util.LinkedList.pop() method is used to remove and return the top element from the stack represented by the LinkedList. The method simply pops out an element present at the top of the stack. This method is similar to removeFirst method in LinkedList.

Syntax:

LinkedListObject.pop()

Parameters: The method does not take any parameter.



Return Value: The method returns the first(top in terms of a stack) value of the stack represented by the LinkedList.

Exception: If there is no element in the stack represented by the LinkedList, the pop method will throw NoSuchElementException().

Below program illustrates the of java.util.LinkedList.pop() method:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to demonstrate pop method in LinkedList
  
import java.util.LinkedList;
  
public class GfG {
    // Main method
    public static void main(String[] args)
    {
  
        // Creating a LinkedList object to represent a stack.
        LinkedList<String> stack = new LinkedList<>();
  
        // Pushing an element in the stack
        stack.push("Geeks");
  
        // Pushing an element in the stack
        stack.push("for");
  
        // Pop an element from stack
        String s = stack.pop();
  
        // Printing the popped element.
        System.out.println(s);
  
        // Pushing an element in the stack
        stack.push("Geeks");
  
        // Printing the complete stack.
        System.out.println(stack);
    }
}

chevron_right


Output:

for
[Geeks, Geeks]

Program 2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to demonstrate pop method in LinkedList
  
import java.util.LinkedList;
  
public class GfG {
    // Main method
    public static void main(String[] args)
    {
  
        // Creating a LinkedList object to represent a stack.
        LinkedList<Integer> stack = new LinkedList<>();
  
        // Pushing an element in the stack
        stack.push(10);
  
        // Pushing an element in the stack
        stack.push(20);
  
        // Pop an element from stack
        Integer ele = stack.pop();
  
        // Printing the popped element.
        System.out.println(ele);
  
        // Pop an element from stack
        ele = stack.pop();
  
        // Printing the popped element.
        System.out.println(ele);
  
        // Throws NoSuchElementException
        ele = stack.pop();
  
        // Throwsca runtime exception
        System.out.println(ele);
  
        // Printing the complete stack.
        System.out.println(stack);
    }
}

chevron_right


Output:

20
10

then it will throw :
Exception in thread "main" java.util.NoSuchElementException
    at java.util.LinkedList.removeFirst(LinkedList.java:270)
    at java.util.LinkedList.pop(LinkedList.java:801)
    at GfG.main(GfG.java:35)


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.