Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

LinkedList addFirst() Method in Java

Last Updated on March 10, 2022 by Ria Pathak


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.

[forminator_quiz id=”4706″]

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.

Leave a Reply

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