Java Program to Get Elements of a LinkedList Last Updated : 27 Nov, 2020 Comments Improve Suggest changes Like Article Like Report Linked List is a linear data structure, in which the elements are not stored at the contiguous memory locations. Here, the task is to get the elements of a LinkedList. 1. We can use get(int variable) method to access an element from a specific index of LinkedList: In the given example, we have used the get(i) method. Here, the method returns the element which is at the i th index. Syntax: LinkedList.get(int index) Parameters: The parameter index is of integer data type that specifies the position or index of the element to be fetched from the LinkedList. Return Value: The method returns the element present at the position specified by the parameter index. Java // Java program to get the elements of Linkedlist import java.io.*; import java.util.LinkedList; class GFG { public static void main(String[] args) { // Creating LinkedList LinkedList<String> gfg = new LinkedList<String>(); // Adding values gfg.add("GEEKS"); gfg.add("FOR"); gfg.add("GEEKS"); System.out.println("LinkedList Elements : "); for (int i = 0; i < gfg.size(); i++) { // get(i) returns element present at index i System.out.println("Element at index " + i + " is: " + gfg.get(i)); } } } OutputLinkedList Elements : Element at index 0 is: GEEKS Element at index 1 is: FOR Element at index 2 is: GEEKS 2. We can use the iterator() method To use this method we have to import java.util.Iterator package.In this method, we can iterate over the LinkedList and then extract the element at the given index accordingly. Java // Java program to iterate over linkedlist // to extract elements of linkedlist import java.io.*; import java.util.LinkedList; import java.util.Iterator; class GFG { public static void main(String[] args) { LinkedList<String> gfg = new LinkedList<String>(); // Adding elements gfg.add("GEEKS"); gfg.add("FOR"); gfg.add("GEEKS"); // Create an object of Iterator Iterator<String> i = gfg.iterator(); System.out.print( "The elements of the input LinkedList: \n"); int j = 0; // has.next() returns true if there is a next // element while (i.hasNext()) { System.out.print("The element at the index " + j + " "); // next() returns the next element String str = i.next(); System.out.print(str); System.out.print(" \n"); ++j; } } } OutputThe elements of the input LinkedList: The element at the index 0 GEEKS The element at the index 1 FOR The element at the index 2 GEEKS 3. We can use ListIterator() method. ListIterator() is a subinterface of Iterator() method.It provides us with the function to access the elements of a list.It is bidirectional that means it allows us to iterate elements of a list in the both the direction.To use this method we have to import java.util.ListIterator. Java // Java program to iterate over the // linkedlist using listIterator() import java.io.*; import java.util.LinkedList; import java.util.ListIterator; class GFG { public static void main(String[] args) { LinkedList<String> gfg = new LinkedList<String>(); // Adding elements gfg.add("GEEKS"); gfg.add("FOR"); gfg.add("GEEKS"); // Create an object of ListIterator ListIterator<String> li = gfg.listIterator(); System.out.print( "The elements of the LinkedList: \n"); // hasNext() returns true if there is next element int j = 0; while (li.hasNext()) { // next() returns the next element System.out.print("The element at the index " + j + " "); System.out.print(li.next()); System.out.print("\n"); ++j; } --j; // Now to show that ListIterator() can traverse in // both the direction System.out.print( "\nThe elements of the LinkedList in Reverse order: \n"); // hasprevious() checks if there is a previous // element while (li.hasPrevious()) { System.out.print("The element at the index " + j + " "); // previous() returns the previous element System.out.print(li.previous()); System.out.print("\n"); --j; } } } OutputThe elements of the LinkedList: The element at the index 0 GEEKS The element at the index 1 FOR The element at the index 2 GEEKS The elements of the LinkedList in Reverse order: The element at the index 2 GEEKS The element at the index 1 FOR The element at the index 0 GEEKS Comment More infoAdvertise with us Next Article Java Program to Get Elements of a LinkedList A anandpriyanshu61 Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 java-LinkedList +1 More Practice Tags : Java Similar Reads Java Program to Implement LinkedList API Linked List is a part of the Collection framework That is present in java.util package. This class is an implementation of the LinkedList data structure which is a linear data structure in which the elements are not stored in contiguous locations and every element is a separate object with a data pa 10 min read Java Program To Delete Middle Of Linked List Given a singly linked list, delete the middle of the linked list. For example, if the given linked list is 1->2->3->4->5 then the linked list should be modified to 1->2->4->5 If there are even nodes, then there would be two middle nodes, we need to delete the second middle element. For example, if g 4 min read Java Program to Iterate LinkedHashSet Elements The LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked List across all elements. When the iteration order is needed to be maintained this class is used. When iterating through a HashSet the order is unpredictable, while a LinkedHashSet lets us iterate through the elements 3 min read Java Program to Get the First and the Last Element of a Linked List A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. The given task is to retrieve the first and the last element of a given linked list. Properties of a Linked ListElements are stored in a non-contiguous manner.Every element is an object whi 4 min read Java Program to Search an Element in a Linked List Prerequisite: LinkedList in java LinkedList is a linear data structure where the elements are not stored in contiguous memory locations. Every element is a separate object known as a node with a data part and an address part. The elements are linked using pointers or references. Linked Lists are pre 5 min read Java Program To Delete Alternate Nodes Of A Linked List Given a Singly Linked List, starting from the second node delete all alternate nodes of it. For example, if the given linked list is 1->2->3->4->5 then your function should convert it to 1->3->5, and if the given linked list is 1->2->3->4 then convert it to 1->3. Recomm 3 min read Java Program to Get Elements By Index from LinkedHashSet LinkedHashSet is a pre-defined class in Java that is similar to HashSet. Unlike HashSet In LinkedHashSet insertion order is preserved. In order to get element by Index from LinkedHashSet in Java, we have multiple ways.Illustration:Input : 2, 3, 4, 2, 7;Processing : index = 4;Output : Element at inde 4 min read Java Program to Implement LinkedHashSet API The LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked List across all elements. When the iteration order is needed to be maintained, this class is used. When iterating through a HashSet, the order is unpredictable, while a LinkedHashSet lets us iterate through the element 4 min read Java Program to Sort the Elements of the Circular Linked List In a circular linked list, every node points to its next node in the sequence but the last node points to the first node in the list. Here, Create a circular linked list and sort the circular linked list in ascending order. Circular linked list before sorting: CIRCULAR LINKED LIST Circular linked li 3 min read Java Program to Implement Triply Linked List Unlike arrays, linked list elements are not stored at a contiguous location; the elements are linked using pointers. In this post, methods to insert a new node in a linked list are discussed. A node can be inserted in three ways either at the front of the linked list or after a given node or at the 8 min read Like