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!

Java.util.LinkedList.get(), getFirst() and getLast() in Java

Last Updated on March 10, 2022 by Ria Pathak

Introduction

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.

get() is the traditional way for retrieving an element at a specific index. Though this is never possible in LinkedList without an entire traversal, it is doable with this approach. There are three variations, all of which are addressed here with Exceptions as well.

get(int index) Method

Syntax: public E get( int index)

Parameters: The index of the element to be returned.

Return Value: The element at the specified index is returned.

Exceptions
  • IndexOutOfBoundsException: If the specified index is out of range, this exception is thrown.

This method takes in the index and returns the element that is present at the specified index. We will just call the get(int index) method.

Input

[Coding, is, Fun], Specified index is 1

Output

is

Explanation

As the element at the specified index (1) is “is”, it is getting printed.

Code Implementation


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

        lst.add("Coding");
        lst.add("is");
        lst.add("Fun");

        System.out.println("The elements present in List are : " + lst);

        System.out.println("Element at the index 1 is : " + lst.get(1));
    }
}
Output

The elements present in List are : [Coding, is, Fun]
Element at the index 1 is : is

Time Complexity: O(n), as we have to traverse to the given index.

Space Complexity: O(1), as only temporary variables are being created.

getFirst() Method

Syntax: public E getFirst()

Return Value: This function returns the list’s first element.

Exceptions
  • NoSuchElementException – This exception is thrown if the given list is empty

This method returns the first element of the LinkedList. We will just call the getFirst() method.

Input

[Coding, is, Fun]

Output

Coding

Explanation

As the first element is “Coding”, it gets printed.

Code Implementation


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

    lst.add("Coding");
    lst.add("is");
    lst.add("Fun");

    System.out.println("The elements present in List are : " + lst);

    System.out.println("Element at index 1 is : " + lst.getFirst());
    }
}
Output

The elements present in List are : [Coding, is, Fun]
Element at index 1 is: Coding

Time Complexity: O(1), as we are just accessing the first element of the LinkedList.

Space Complexity: O(1), as only temporary variables are being created.

getLast() Method

Syntax: public E getLast()

Return Value: This function returns the list’s last element.

Exceptions
  • NoSuchElementException – This exception is thrown if the given list is empty

This method returns the LinkedList’s last element. We will just call the getLast() method.

Input

[Coding, is, Fun]

Output

Fun

Explanation

As the last element is “Fun”, it gets printed.

Code Implementation


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

        lst.add("Coding");
        lst.add("is");
        lst.add("Fun");

        System.out.println("The elements present in the List are : " + lst);

        System.out.println("Element present at last index is : " + lst.getLast());
    }
}
Output

The elements present in the List are : [Coding, is, Fun]
Element present at last index is : Fun

Time Complexity: O(1), as we are just accessing the last element of the LinkedList. We have a constant time complexity here because the LinkedList used is a doubly-linked list and hence it has a reference to its tail.

Space Complexity: O(1), as only temporary variables are being created.

So, in this article, we have tried to explain how to use get(), getFirst() and getLast() methods 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 *