LinkedList addFirst() Method in Java


One of the most crucial data structures to learn while preparing for interviews is the linked list. In a coding interview, having a thorough understanding of Linked Lists might be a major benefit.

In Java, the java.util.LinkedList.addFirst() function is used to place a specified element at the start of a LinkedList.

LinkedList addFirst() Method

Syntax: void addFirst( Object X)

Parameters: This method accepts a single parameter, the element to be added to the beginning of the list.

Return Value: This function has no return value.

This function takes in an element and adds it to the beginning of the LinkedList. We will just simply call the LinkedList.addFirst(Object X) method.

[Coding, is, Fun], Element to be added at the beginning of the list: DSA.

[DSA, Coding, is, Fun]

Explanation: As we can see, the specified element DSA has been added to the beginning of the LinkedList.

Code Implementation

import java.util.LinkedList;
public class PrepBytes {
    public static void main(String args[])
        LinkedList list = new LinkedList();

        System.out.println("The list is: " + list);

        System.out.println("The new List is: " + list);


The list is: [Coding, is, Fun]
The new List is: [DSA, Coding, is, Fun]

Time Complexity: O(1), as the specified element is only getting added to the beginning of the LinkedList.

So, in this article, we have tried to explain how to use the LinkedList addFirst() method in java. Java Collection Framework is very important when it comes to coding interviews. If you want to solve more questions on Linked List, which are curated by our expert mentors at PrepBytes, you can follow this link Linked List.

Previous post Check if a doubly-linked list of characters is palindrome or not
Next post LinkedHashMap get() Method in Java

Leave a Reply

Your email address will not be published. Required fields are marked *