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
- LinkedList clone() Method in Java In Java, the clone() method of LinkedList, creates a shallow copy which means the structure is duplicated but the objects inside the list are shared between the original and the copied list. So, the cloned list will have the same elements as the original list. Syntax of Java LinkedList clone() Metho 1 min read
- LinkedList contains() Method in Java In Java, the contains() method of LinkedList is used to check whether an element is present in a LinkedList or not. It takes the element as a parameter and returns True if the element is present in the list. Syntax of Java LinkedList contains() Method boolean contains(Object element);Parameter: The 2 min read
- LinkedList descendingIterator() Method in Java In Java, the descendingIterator() method of LinkedList is used to return an iterator that allows to traverse the list in reverse order. Example 1: This example demonstrates how to use descendingIterator() method with Strings in a LinkedList. [GFGTABS] Java // Java program to use // descendingIterato 3 min read
- LinkedList element() Method in Java In Java, the element() method of the LinkedList class is used to retrieve the first element in the list without removing it. The first element of the LinkedList is known as the head. Example 1: Here, we use the element() method to retrieve the first element of the LinkedList of Integers, without rem 2 min read
- Java.util.LinkedList.get(), getFirst(), getLast() in Java In Java, the LinkedList class provides several methods for accessing the elements in the list. Here are the methods for getting the elements of a LinkedList: get(int index): This method returns the element at the specified position in the LinkedList. The index parameter is zero-based, so the first e 5 min read
- LinkedList getLast() Method in Java In Java, the getLast() method in the LinkedList class is used to retrieve the last element of the list. Example 1: Here, we use the getLast() method to retrieve the last element of the list. [GFGTABS] Java // Java Program to Demonstrate the // use of getLast() in LinkedList import java.util.LinkedLi 2 min read
- LinkedList indexOf() Method in Java In Java, the indexOf() method of the LinkedList class is used to find the index of the first occurrence of the specified element in the list. Syntax of LinkedList indexOf() Method public int indexOf(Object o); Parameter: This method takes an object "o" as an argument. Return type: It returns the ind 2 min read
- LinkedList lastIndexOf() Method in Java In Java, the lastIndexOf() method of LinkedList is used to find the last occurrence of the specified element in the list. If the element is present it will return the last occurrence of the element, otherwise, it will return -1. Syntax of LinkedList lastIndexOf() Method public int lastIndexOf(Object 2 min read
- LinkedList listIterator() Method in Java In Java, the listIterator() method of the LinkedList class returns a ListIterator that allows us to iterate over the elements of the list. Example: [GFGTABS] Java // Java Program to Demonstrate the // use of listIterator() in LinkedList import java.util.LinkedList; import java.util.ListIterator; pub 3 min read
- Java.util.LinkedList.offer(), offerFirst(), offerLast() in Java In Java, the LinkedList class is present in java.util package, and it also has different methods that do the work of flexible addition of elements and help addition both at the front and back of the list. In this article, we cover the LinkedList offer(), offerFirst(), and offerLast() methods. These 5 min read