Java Program to Get the First and the Last Element of a Linked List Last Updated : 06 Sep, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 which contains a pointer to the next element.Example to Get the First and the Last Element of a Linked ListInput: [2, 5, 5, 7, 10, 6]Output: First element is : 2 Last element is : 6 Input: [1]Output: First element is : 1 Last element is : 1 1. Using in-built librariesUse the pre-built LinkedList class in the java.util package to build a Linked List and use the pre-defined methods to fetch the respective values. Below is the implementation of the above approach: Java // Java Program to get the first and the last element of a // Linked List // Importing the Linked List class from the util package import java.util.LinkedList; class AccessFirstAndLastElements { public static void main(String[] args) { // Initializing the Linked List LinkedList<Integer> ll = new LinkedList<>(); // Adding elements to the Linked List ll.add(2); ll.add(5); ll.add(5); ll.add(7); ll.add(10); ll.add(6); // Getting the first element System.out.println("First Element is : " + ll.getFirst()); // Getting the last element System.out.println("Last Element is : " + ll.getLast()); } } OutputFirst Element is : 2 Last Element is : 6The complexity of the above method:Time Complexity: getFirst() takes O(1) time and getLast() takes O(1) time.Auxiliary Space: O(1), As constant extra space is used. 2. Without using in-built methods Create a generic Node class.Create our very own Linked List class using the Node class.Create the required add(), getFirst() and getLast() methods.Initialize the Linked List.Use the respective get methods to fetch the values.Below is the implementation of the above approach: Java // Java program to get the first and last element from a // Linked List // A Generic Node class is used to create a Linked List class Node<E> { // Data Stored in each Node of the Linked List E data; // Pointer to the next node in the Linked List Node<E> next; // Node class constructor used to initializes the data // in each Node Node(E data) { this.data = data; } } class LinkedList<E> { // Points to the head of the Linked List // i.e the first element Node<E> head; int size = 0; // Addition of elements to the tail of the Linked List public void add(E element) { // Checks whether the head is created else creates a // new one if (head == null) { head = new Node<>(element); size++; return; } // The Node which needs to be added at // the tail of the Linked List Node<E> add = new Node<>(element); // Storing the instance of the // head pointer Node<E> temp = head; // The while loop takes us to the tail of the Linked // List while (temp.next != null) { temp = temp.next; } // New Node is added at the tail of // the Linked List temp.next = add; // Size of the Linked List is incremented as // the elements are added size++; } // Retrieves the first element of the Linked List public E getFirst() throws Exception { // Throws an Exception if the List is empty if (head == null) { throw new Exception( "No elements found in Linked List"); } // Returns the first element return head.data; } // Retrieves the last element of the Linked List public E getLast() throws Exception { // Throws an Exception if the List is empty if (head == null) { throw new Exception( "No elements found in Linked List"); } Node<E> temp = head; // The while loop takes us to the tail of the Linked // List while (temp.next != null) { temp = temp.next; } // Returns the last element return temp.data; } } class AccessFirstAndLastElements { public static void main(String[] args) throws Exception { // Initializing the Linked List LinkedList<Integer> ll = new LinkedList<>(); // Adding elements to the Linked List ll.add(2); ll.add(5); ll.add(5); ll.add(7); ll.add(10); ll.add(6); // Getting the first element System.out.println("First Element is : " + ll.getFirst()); // Getting the last element System.out.println("Last Element is : " + ll.getLast()); } } OutputFirst Element is : 2 Last Element is : 6The complexity of the above method:Time Complexity: O(1) Auxiliary space: O(1) Comment More infoAdvertise with us Next Article Java Program to Get Elements of a LinkedList C coder_srinivas Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Practice Tags : Java Similar Reads Java Program to Get Elements of a LinkedList 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 4 min read Java Program For Moving Last Element To Front Of A Given Linked List Write a function that moves the last element to the front in a given Singly Linked List. For example, if the given Linked List is 1->2->3->4->5, then the function should change the list to 5->1->2->3->4. Algorithm: Traverse the list till the last node. Use two pointers: one t 3 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 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 Delete a Node From the Ending of the Circular Linked List In this article, we will learn about deleting a node from the ending of a circular linked list. Consider the linked list as shown below:Â Example:Input : 5->3->4->(head node)Output: 5->3->(head node)We will first initialize the list and add some data into it with the addNode() method a 3 min read Java Program For Finding The Middle Element Of A Given Linked List Given a Singly linked list, find the middle of the linked list. If there are even nodes, then there would be two middle nodes, we need to print the second middle element. Example of Finding Middle Element of Linked ListInput: 1->2->3->4->5 Output: 3Â Input: 1->2->3->4->5-> 6 min read Like