LinkedList push() Method in Java (original) (raw)

Last Updated : 18 Dec, 2024

In Java, the **push() method of the LinkedList class is used to push an element at the starting(top) of the stack represented by LinkedList. This is similar to the addFirst() method of LinkedList. This method simply inserts the element at the first position or top of the LinkedList.

**Syntax of LinkedList push() Method

public void push(Object element);

**Parameters: This method accepts one parameter **element of object type and represents the element to be inserted. The type “Object” should be of the same Stack represented by the LinkedList.

**Example: Here, we use the **push() method **to push elements at the beginning of the LinkedList **of Integers.

java `

// Push Elements in LinkedList of Integers import java.util.LinkedList; public class Geeks {

public static void main(String[] args) {

    // Create an empty list
    LinkedList<Integer> l = new LinkedList<>();

    // Pushing an element at the 
    // beginning of the list
    l.push(30);

    // Pushing an element at the 
    // beginning of the list
    l.push(20);

    // Pushing an element at the 
    // beginning of the list
    l.push(10);

    System.out.println("LinkedList is: " + l);
}

}

`

Output

Linked List is : [10, 20, 30]

**Note: We can also use the **push() method with LinkedList of Strings, Objects etc.

Similar Reads