Java LinkedList Class with Example | LinkedList in Java Declaration, Creation, Methods

The LinkedList in Java is a class present in java.util package. LinkedList class is a part of the Java Collection Framework. The LinkedList extends AbstractSequentialList and implements the List interface and Deque interface. LinkedList class in Java provides a linked-list data structure. Java LinkedList class uses the concept of the doubly linked list to store the elements. In the doubly linked list, we can add or remove the elements from both sides.

Java LinkedList is the best choice if we want to perform operations such as insertion or deletion in the middle of the list. It also has a disadvantage if we want to access a node, the nodes cannot be accessed directly instead we need to start from the head and reach the node we want to access.

Java LinkedList Class with Example 1

This Tutorial on LinkedList Class in Java covers the topics

Constructors in Java LinkedList Class

Java LinkedList class has two constructors which are shown below

1. LinkedList(): The first constructors used to builds an empty linked list.

2. LinkedList(Collection c): The second constructor is used to builds a linked list that is initialized with the elements of the collection c.

LinkedList Class Declaration

public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>, Deque<E>, Cloneable, Serializable

How to Create a LinkedList?

We can create a LinkedList by writing a simple statement which is shown below:

LinkedList list = new LinkedList();

This statement creates a linked list with the name list which is of type “String”. The type determines which type of elements the Linked List should contain. Since it is of type String, the elements that are going to be added in this list will be of String type.

Methods of Java LinkedList

Method Description
boolean add(E e) This method is used to add specified elements at the end of the list.
void add(int index, E element) This method is used to add the specified element at a given index.
boolean addAll(Collection c) This method is used to add all the elements of the specified collection c to the end of the list.
boolean addAll(int index, Collection c) This method is used to add all the elements of the specified collection c to the given index of the list.
void addFirst(E e) This method is used to add the element to the beginning of the list
void addLast(E e) This method is used to add the element to the end of the list
void clear() This method is used to remove all the elements from a list.
boolean contains(E e) This method is used to find out the specified element from a list, it returns true if the list contains a specified element otherwise it returns false.
E get(int index) This method is used to returns the element at the specified position from the list.
E getFirst() This method is used to returns the first element from the list.
E getLast() This method is used to returns the last element from the list
int indexOf(Element e) This method is used to returns the index of the specified element. It returns -1 if the list does not contain a specified element.
int lastIndexOf(Element e) This method is used to returns the index of the last occurrence of the specified element from the list. It returns -1 if the list does not contain a specified element.
E remove() This method is used to remove the first element from a list.
E remove(int index) This method is used to remove the element from the list which is present at a given index.
E removeFirst() This method is used to remove the first element from the list.
E removeLast() This method is used to remove the last element from the list.
boolean removeFirstOccurrence(Object item) This method is used to remove the first occurrence of the specified item when traversing the elements from head to tail.
boolean removeLastOccurrence(Object item) This method is used to remove the last occurrence of the specified item when traversing the elements from head to tail.
boolean remove(Object o) This method is used to removes the first occurrence of the specified element from the list
int size() This method returns the number of elements which is present in the list.
E set(int index, Element e) This method is used to replace the item of the specified index with the given value.

The four commonly used LinkedList Operators in this tutorial and are as below

  • Add Elements
  • Access Elements
  • Change Elements
  • Remove Elements

We have provided Sample Example Programs for all these methods in the below modules.

Also, See:

Java LinkedList Example for Adding Elements

We can add elements to a linked list by using the following methods add(),
add(int index, Element e), addFisrt(Element e), and addLast(Element e). If you want to add an element at the end of the list then you can use the addLast() method. If you want to add an element at the specified position in the list then you can use add(int index, Element e).

Let’s understand this with a simple example:

import java.util.
class Example{
public static void main(String args[]){

//creating a linked list
LinkedList list = new LinkedList();

//adding elements to the list
list.add("Amit");
list.add("Rahul");
list.add("Raam");
list.add("Shyam");
list.add("Tom");
list.add("Karan");

//displaying elements
System.out.println("After adding the above elements the list will be:n" +list);

// add element at 3 position.
list.add(2, "Prashant");

//displaying elements after adding at a given index.
System.out.println("After adding the element at a specified index:n" +list);

//adding an element at first position of list
list.addFirst("Vijay");
System.out.println("After adding an element at first position:n" +list);

//adding an element at last position of list
list.addLast("Ramesh");
System.out.println("After adding an element at last position:n" +list);
}
}

Output:

Java LinkedList Class with Example 2

Java LinkedList Example for Removing Elements

We can remove elements from a linked list by using the following methods remove(), remove(int index), removeFirst(Element e), and removeLast(Element e). If you want to remove an element at the end of the list then you can use the removeLast() method. If you want to remove an element at the specified position in the list then you can use remove(int index).

Let’s understand this with a simple example:

import java.util.*;
class Example{
public static void main(String args[]){

//creating a linked list
LinkedList list = new LinkedList();

//adding elements to the list
list.add("Vijay");
list.add("Amit");
list.add("Shyam");
list.add("Tom");
list.add("Amit");
list.add("Tom");
list.add("Karan");

//displaying elements
System.out.pritnln("After adding the above elements the list will be:n" +list);

// removing an element from the 3 position.
list.remove(2);

//displaying elements after removing the 3rd element
System.out.println("After removing the 3rd element:n" + list);

//removing first element
list.removeFirst();
System.out.println("After removing first element:n" +list);

//removing last element
list.removeLast();
System.out.println("After removing last element:n" +list);

//removing first occurrence element
list.removeFirstOccurrence("Amit");
System.out.println("After removing first occurrence element:n" +list);

//removing last occurrence element
list.removeLastOccurrence("Tom");
System.out.println("After removing last occurrence element:n" +list);

}
}

Output:

Java LinkedList Class with Example 3

Java LinkedList Example to get the First and Last Element

We can use the getFirst() method to get the first element from a list and getLast() method to get the last element from the list.

Let’s understand this with an example:

import java.util.*;
class Example{
public static void main(String args[]){

//creating a linked list
LinkedList list = new LinkedList();

//adding elements to the list
list.add("Amit");
list.add("Rahul");
list.add("Raam");
list.add("Shyam");
list.add("Tom");

//retrurns first element from a list
Object firstElement = list.getFirst()
System.out.println("The first element of the list is:" +firstElement);

//retrurns last element from a list
Object lastElement = list.getLast()
System.out.println("The last element of the list is:" +lastElement);
}

}

Output:

The first element of the list is: Amit
The last element of the list is: Tom

Java LinkedList Example to get the Element from the Specified Index

We can use get(int index) method to get the element from the specified index of a linked list. Let’s understand this with an example:

import java.util.*;
class Example{
public static void main(String args[]){

//creating a linked list
LinkedList list = new LinkedList();

//adding elements to the list
list.add("Amit");
list.add("Rahul");
list.add("Raam");
list.add("Shyam");
list.add("Tom");

//retreive 3rd element
Object element = list.get(2);
System.out.println("The element from the specified index is:" +element);
}

}

Output:

The element from the specified index is: Raam

ArrayList vs LinkedList in Java

1. ArrayList is implemented with the concept of dynamic arrays while LinkedList is implemented with the concept of a doubly linked list.

2. ArrayList is slow because it internally uses the concept of arrays. If we want to remove an element from an array, all the bits are shifted in memory.

3. LinkedList is faster than ArrayList because it uses the concept of a doubly-linked list, so bit shifting is not required in memory.

4. ArrayList is best if we want to store and access the data while LinkedList is best if we want to manipulate the data.

5. ArrayList acts as a list because it implements only a list interface while LinkedList acts as a list as well as queue because it implements list and Dequeue both interface.

Leave a Comment