Java Program to Access elements from a LinkedList



The Linked List in a Java is a data structure that is used for storing data in linear order. It has a series of node where each node contains two parts: data and a reference to the next node in the list. The first node is called the head and the last node is called the tail.

In this article, we will learn how to access elements from a LinkedList in Java.

Following are the ways to access elements from a LinkedList in Java:

Accessing Elements from a LinkedList Using get() Method

The get() method of the LinkedList class is used to access an element at a specific index in the list. It takes an integer index as a parameter and returns the element at that index.

Example

In the following example, we will create a LinkedList and access its elements using the get() method:

import java.util.LinkedList;
public class AccessLinkedList {
   public static void main(String[] args) {
      LinkedList<String> list = new LinkedList<>();
      
      list.add("Java");
      list.add("Python");
      list.add("JavaScript");
      list.add("C++");
      
      System.out.println("Original LinkedList: " + list);
      
      String firstElement = list.get(0);
      String secondElement = list.get(1);
      
      System.out.println("First Element: " + firstElement);
      System.out.println("Second Element: " + secondElement);
   }
}

Output

Following is the output of the above code:

Original LinkedList: [Java, Python, JavaScript, C++]
First Element: Java
Second Element: Python

Accessing Elements from a LinkedList Using getFirst() Method

The getFirst() method of the LinkedList is useful for accessing the first element of the linkedlist. It takes nothing as a parameter and returns the first element of the list.

Example

In the following example, we will create a LinkedList and access its first element using the getFirst() method:

import java.util.LinkedList;
public class AccessLinkedListFirst {
   public static void main(String[] args) {
      LinkedList<String> list = new LinkedList<>();
      
      list.add("Java");
      list.add("Python");
      list.add("JavaScript");
      list.add("C++");
      
      System.out.println("Original LinkedList: " + list);
      
      String firstElement = list.getFirst();
      
      System.out.println("First Element: " + firstElement);
   }
}

Output

Following is the output of the above code:

Original LinkedList: [Java, Python, JavaScript, C++]
First Element: Java

Accessing Elements from a LinkedList Using getLast() Method

Similar to the getFirst() method, the getLast() method of the LinkedList is used for accessing the last element of the linkedlist. It also takes nothing as a parameter and returns the last element of the list.

Example

In the following example, we will create a LinkedList and access its last element using the getLast() method:

import java.util.LinkedList;
public class AccessLinkedListLast {
   public static void main(String[] args) {
      LinkedList<String> list = new LinkedList<>();
      
      list.add("Java");
      list.add("Python");
      list.add("JavaScript");
      list.add("C++");
      
      System.out.println("Original LinkedList: " + list);
      
      String lastElement = list.getLast();
      
      System.out.println("Last Element: " + lastElement);
   }
}

Output

Following is the output of the above code:

Original LinkedList: [Java, Python, JavaScript, C++]
Last Element: C++
Aishwarya Naglot
Aishwarya Naglot

Writing clean code… when the bugs aren’t looking.

Updated on: 2025-08-20T12:26:31+05:30

437 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements