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 List
- Elements 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 List
Input: [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 libraries
Use 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
import java.util.LinkedList;
class AccessFirstAndLastElements {
public static void main(String[] args)
{
LinkedList<Integer> ll = new LinkedList<>();
ll.add( 2 );
ll.add( 5 );
ll.add( 5 );
ll.add( 7 );
ll.add( 10 );
ll.add( 6 );
System.out.println( "First Element is : "
+ ll.getFirst());
System.out.println( "Last Element is : "
+ ll.getLast());
}
}
|
Output
First Element is : 2
Last Element is : 6
The 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
class Node<E> {
E data;
Node<E> next;
Node(E data) { this .data = data; }
}
class LinkedList<E> {
Node<E> head;
int size = 0 ;
public void add(E element)
{
if (head == null ) {
head = new Node<>(element);
size++;
return ;
}
Node<E> add = new Node<>(element);
Node<E> temp = head;
while (temp.next != null ) {
temp = temp.next;
}
temp.next = add;
size++;
}
public E getFirst() throws Exception
{
if (head == null ) {
throw new Exception(
"No elements found in Linked List" );
}
return head.data;
}
public E getLast() throws Exception
{
if (head == null ) {
throw new Exception(
"No elements found in Linked List" );
}
Node<E> temp = head;
while (temp.next != null ) {
temp = temp.next;
}
return temp.data;
}
}
class AccessFirstAndLastElements {
public static void main(String[] args) throws Exception
{
LinkedList<Integer> ll = new LinkedList<>();
ll.add( 2 );
ll.add( 5 );
ll.add( 5 );
ll.add( 7 );
ll.add( 10 );
ll.add( 6 );
System.out.println( "First Element is : "
+ ll.getFirst());
System.out.println( "Last Element is : "
+ ll.getLast());
}
}
|
Output
First Element is : 2
Last Element is : 6
The complexity of the above method:
Time Complexity: O(1)
Auxiliary space: O(1)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
06 Sep, 2023
Like Article
Save Article