Complete Guide on Linked List in Java | 2022

In this article, We will come to know about a data structure called “Linked List” and its various in-built function in Java. Before moving directly towards inbuilt function let us take a small introduction to Linked List and its types.

What is a Linked List in Java?

A Linked List is a linear data structure, Which stores objects at non – continuous locations in heap memory. Objects are called Node.

Each Node contains two things one is Data and the other is a pointer. As these objects are at non-continuous locations, therefore, Each Node is connected to its next Node via the next pointer(called references in Java), Which points to the storage location of the next Node. For example and more see the image below.

Types of Linked Lists

Linked Lists are divided into the below 4 types based on their different properties :

Singly Linked List

That type of Linked List, Which contains the data part and next pointer in each Node is called a Singly Linked List. Traversal in this type of Linked List is uni-directional Because you can only in one direction with help of the next pointer.

The first node of the list is called as Head. The next pointer of the last node is NULL, Because the List ends here, Formally no further nodes after the last node.

The below image is a visual explanation of the Singly Linked List.

Singly Linked List in Java
Singly Linked List in Java

Doubly Linked List

A Doubly Linked List is a type of Linked List in which each Node contains data, next and previous pointer. Traversal is bi-directional because you can go from the current node to its next node via the next pointer and the previous as well from the previous pointer of the current node.

The previous pointer of the Head and the next pointer of the last Node are NULL. See the image below for a clear understanding of the structure.

Doubly Linked List in Java
Visual explanation of the Doubly Linked list

Circular Linked List

Circular Linked List is a type of Linked List in which the Last Node’s next pointer indicates the storage location of the First Node. Formally, Circular Linked List has no start and end. The below image is a visualization example of a Circular Linked List.

Doubly Circular Linked List in Java
Circular Linked List in Java

Doubly Circular Linked List

Doubly Circular Linked List is a more optimized version of Simple Circular Linked List. Each Node contains data, next and prev pointer.

The next pointer of the Last node makes the reference to the First Node via the next pointer, While the prev pointer of the First node makes a reference to the Last Node.

Doubly Circular Linked List
Doubly Circular Linked List

So far till now, We know about the structure and types of Linked Lists. Now we will discuss some in-built methods for each type of linked list.

In-Built Methods of Linked List

To create an empty Singly Linked List we have to initialize an object of a linked List using the constructor LinkedList(). Below is the implementation of the code for creating an empty Linked List as an instance name list.

//{ Driver Code Startspackage newPackage;
package newPackage;
import java.util.*;
import java.io.*;
import java.lang.*;

class BeeTechnical
{
    public static void main(String args[])
    {
           LinkedList<Integer> list = new LinkedList<>();  
           System.out.println(list);
    }
 
}

Output :

The output of the empty Linked List
The output of the empty Linked List

As we can see that we successfully created the Linked List of Integer data type by using the LinkedList() constructor.

As there is no element present in the list, Therefore the list in the output is empty.

Add() Method in Linked List

This method is used to insert the element at the end position of the list. Formally, appends the argument e of E data type in the list.

//{ Driver Code Startspackage newPackage;
package newPackage;
import java.util.*;
import java.io.*;
import java.lang.*;

class BeeTechnical
{
    public static void main(String args[])
    {
           LinkedList<Integer> list = new LinkedList<>();
           list.add(5);
           System.out.println("5 added in the list");
           list.add(7);
           System.out.println("7 append in the list");
           System.out.println(list);
    }
 
}

Output :

The output of add() method in a linked list
The output of add() method in linked list

We can see that 7 is appended after element 5 in the list.

Add Element at Specific Position in Linked List

This method is used to insert an element at the specified position in the Linked List.

//{ Driver Code Startspackage newPackage;
package newPackage;
import java.util.*;
import java.io.*;
import java.lang.*;

class BeeTechnical
{
    public static void main(String args[])
    {
           LinkedList<Integer> list = new LinkedList<>();
           list.add(5);
           list.add(7);
           System.out.println("List before adding 2 : "+list);
           list.add(1,2);
           System.out.println("2 inserted at index 1");
           System.out.println("List after adding 2 : "+list);
    }
 
}

Output :

output of add(int index, element) method
output of add(int index, element) method

In the output, we can see that 2 is inserted at the index 1, by using add(int index, element) method.

Add Collection to the Linked List

This method is used to append all the elements of the passed collection as an argument in the addAll() method of the E data type. Formally, We can append all the elements of the second list into the first list.

//{ Driver Code Startspackage newPackage;
package newPackage;
import java.util.*;
import java.io.*;
import java.lang.*;

class BeeTechnical
{
    public static void main(String args[])
    {
           LinkedList<Integer> list_1 = new LinkedList<>();
           list_1.add(1);
           list_1.add(2);
           System.out.println("List 1 : "+list_1);
           LinkedList<Integer> list_2 = new LinkedList<>();           
           list_2.add(3);
           list_2.add(4);
           System.out.println("List 2 : "+list_2);
           
           //Adding all elements of list_2 into list_1 by using addAll() method  
           list_1.addAll(list_2);
           System.out.println("list_1 after adding all elements of list_2 : "+list_1);
    }
 
}

Output :

Complete Guide on Linked List in Java | 2022 1
output of addAll(Collection<E> e method)

We can see in the output that all the elements of list_2 are appended in the list_1 by using the allAll() method.

Add Collection at Specific Index in Linked List

This method is used to insert all the elements of the passed collection as an argument in the addAll() method of the E data type at the specified index. Formally, We can append all the elements of the second list into the first list.

//{ Driver Code Startspackage newPackage;
package newPackage;
import java.util.*;
import java.io.*;
import java.lang.*;

class BeeTechnical
{
    public static void main(String args[])
    {
           LinkedList<Integer> list_1 = new LinkedList<>();
           list_1.add(1);
           list_1.add(2);
           System.out.println("List 1 : "+list_1);
           LinkedList<Integer> list_2 = new LinkedList<>();           
           list_2.add(3);
           list_2.add(4);
           System.out.println("List 2 : "+list_2);
           
           //Adding all elements of list_2 into list_1 on index 1 by using addAll() method  
           list_1.addAll(1,list_2);
           System.out.println("list_1 after adding all elements of list_2 on index 1 : "+list_1);
    }
 
}

Output :

Complete Guide on Linked List in Java | 2022 2
output of addAll(int index, Collectiton<E> e)

In the output, We can see that elements 3, and 4 of list_2 are inserted into list_1 from index 1.

Add Element at First Position in Linked List

AddFirst(E e) method is used to add element e of the E data type at the Head(very First element) of the list.

//{ Driver Code Startspackage newPackage;
package newPackage;
import java.util.*;
import java.io.*;
import java.lang.*;

class BeeTechnical
{
    public static void main(String args[])
    {
           LinkedList<Integer> list_1 = new LinkedList<>();
           list_1.add(50);
           list_1.add(46);
           System.out.println("list_1 : "+list_1);
         
           //Adding elements 60 at first index of list by using addFirst() method 
           list_1.addFirst(60);
           System.out.println("list_1 after adding 60 at Head of the list 1 : "+list_1);
    }
 
}

Output :

Add Element at First Position in Linked List
output of addFirst() method

Element 60 is added at the Head of list_1 using the addFirst() method.

Add Element at Last Position in Linked List

addLast(E e) method is used to insert an element at the last position of the list.

//{ Driver Code Startspackage newPackage;
package newPackage;
import java.util.*;
import java.io.*;
import java.lang.*;

class BeeTechnical
{
    public static void main(String args[])
    {
           LinkedList<Integer> list_1 = new LinkedList<>();
           list_1.add(50);
           list_1.add(46);
           System.out.println("list_1 : "+list_1);
         
           //Adding elements 60 at last position of list by using addLast() method 
           list_1.addLast(60);
           System.out.println("list_1 after adding 60 at last position of the list 1 : "+list_1);
    }
 
}

Output :

output of addLast() method
output of addLast() method

Element 60 is added at the last position of the list by using the addLast() method.

Find the Size of the Linked List

size() method that returns the current size of the List.

//{ Driver Code Startspackage newPackage;
package newPackage;
import java.util.*;
import java.io.*;
import java.lang.*;

class BeeTechnical
{
    public static void main(String args[])
    {
           LinkedList<Integer> list = new LinkedList<>();
           list.add(50);
           list.add(46);
           System.out.println("size of the list is : "+list.size());
    }
 
}

Output :

output of size() method
output of size() method

In the output, it can be seen that size of the list is 3.

Remove Elements from the Linked List

the clear() method is used to remove all the elements present in the list currently.

//{ Driver Code Startspackage newPackage;
package newPackage;
import java.util.*;
import java.io.*;
import java.lang.*;

class BeeTechnical
{
    public static void main(String args[])
    {
           LinkedList<Integer> list = new LinkedList<>();
           list.add(50);
           list.add(46);
           System.out.println("list : "+list);
           list.clear();
           System.out.println("list after using clear() method : "+list);
    }
 
}

Output :

Remove elements from the linked list
output of clear() operation

Clone Linked List Elements

Object cloning refers to the creation of an exact copy of an object. It creates a new instance of the class of the current object and initializes all its fields with exactly the contents of the corresponding fields of this object.

//{ Driver Code Startspackage newPackage;
package newPackage;
import java.util.*;
import java.io.*;
import java.lang.*;

class BeeTechnical
{
    public static void main(String args[])
    {
           LinkedList<Integer> list_1 = new LinkedList<>();
           list_1.add(50);
           list_1.add(46);
           LinkedList<Integer> list_2 = (LinkedList<Integer>) list_1.clone();
           System.out.println("Copied elements in list_2 from list_1 : "+list_2);
    }
 
}

Output :

Clone Linked List elements using Clone method
output of clone() function

Check Existing Elements in Linked List

contains(E e) method returns true if the element is present in the list else returns false.

Code :

//{ Driver Code Startspackage newPackage;
package newPackage;
import java.util.*;
import java.io.*;
import java.lang.*;

class BeeTechnical
{
    public static void main(String args[])
    {
           LinkedList<Integer> list = new LinkedList<>();
           list.add(5);
           list.add(6);
           System.out.println("List contains 6  :  "+list.contains(6));
           System.out.println("List contains 8  :  "+list.contains(8));
    }
 
}

Output :

output of contains() method
output of contains() method

Find Elements in Linked List

get(int index) method is used to get elements at the specified index in the list, without removing it.

Code :

//{ Driver Code Startspackage newPackage;
package newPackage;
import java.util.*;
import java.io.*;
import java.lang.*;

class BeeTechnical
{
    public static void main(String args[])
    {
           LinkedList<Integer> list = new LinkedList<>();
           list.add(5);
           list.add(6);
           list.add(1);
           list.add(10);
           list.add(2);
           System.out.println(list);
           System.out.println("Element present at index 2 :  "+list.get(2));
    }
 
}

Output :

output of get() method in linked list
output of get() method

Find the first or Last Elements in Linked List

getFirst() and getLast() : net first() and getLast() methods are used to get elements at First and Last position in the list respectively.

//{ Driver Code Startspackage newPackage;
package newPackage;
import java.util.*;
import java.io.*;
import java.lang.*;

class BeeTechnical
{
    public static void main(String args[])
    {
           LinkedList<Integer> list = new LinkedList<>();
           list.add(5);
           list.add(6);
           list.add(1);
           list.add(10);
           list.add(2);
           System.out.println(list);
           System.out.println("First element of the list :  "+list.getFirst());
           System.out.println("Last element of the list :  "+list.getLast());
    }
 
}

Output :

output of getFirst() and getLast() in the linked list
output of getFirst() and getLast()

Find the index of the elements in the Linked List

indexOf(E e) and lastIndexOf(E e) : IndexOf(E e) and lastIndexOf(E e) are used to get index of first occurrence and last occurrence of an element e of data type E respectively.

//{ Driver Code Startspackage newPackage;
package newPackage;
import java.util.*;
import java.io.*;
import java.lang.*;

class BeeTechnical
{
    public static void main(String args[])
    {
           LinkedList<Integer> list = new LinkedList<>();
           list.add(5);
           list.add(1);
           list.add(2);
           list.add(1);
           list.add(9);
           list.add(1);
           System.out.println(list);
           System.out.println("First index of element 1 :  "+list.indexOf(1));
           System.out.println("Last index of element 1 :  "+list.lastIndexOf(1));
    }
 
}

Output :

output of indexOf() and lastIndexOf() method
output of indexOf() and lastIndexOf() method

Find the First Element in Linked List

element() and peek(): Both methods return the element present at the head of the list, without removing it.

Code :

//{ Driver Code Startspackage newPackage;
package newPackage;
import java.util.*;
import java.io.*;
import java.lang.*;

class BeeTechnical
{
    public static void main(String args[])
    {
           LinkedList<Integer> list = new LinkedList<>();
           list.add(5);
           list.add(1);
           list.add(2);
           list.add(1);
           list.add(9);
           list.add(1);
           System.out.println(list);
           System.out.println("Head element using peek() method :  "+list.peek());
           System.out.println("Head element using element() method :  "+list.element());
    }
 
}

Output :

output of peek() and element method() in linked list
output of peek() and element method()

Remove Elements from the Linked List in Java

remove(), removeFirst() and removeLast() : Both the methods remove(), removeFirst() removes the element present at head of the list, While removeLast() removes the element present at last position in the list.

//{ Driver Code Startspackage newPackage;
package newPackage;
import java.util.*;
import java.io.*;
import java.lang.*;

class BeeTechnical
{
    public static void main(String args[])
    {
           LinkedList<Integer> list = new LinkedList<>();
           list.add(5);
           list.add(1);
           list.add(2);
           list.add(1);
           list.add(9);
           list.add(1);
           System.out.println(list);
           System.out.println("Removing 5 at head by remove() function :  "+list.remove());
           System.out.println(list);
           System.out.println("Removing 1 at head by removeFirst() function :  "+list.removeFirst());
           System.out.println(list);
           System.out.println("Removing 1 at last position by removeLast() function  "+list.removeLast());
           System.out.println(list);
    }
 
}

Output :

output of remove(), removeFirst() and removeLast() in Java  Linked List
output of remove(), removeFirst() and removeLast()

Remove Elements from Specific Index from the Linked List in Java

remove(int index) and remove(E e): remove(int index) removes the element at a specified position of the list. On the contrary, remove(E e) removes the first occurrence of the element e.

Code :

//{ Driver Code Startspackage newPackage;
package newPackage;
import java.util.*;
import java.io.*;
import java.lang.*;

class BeeTechnical
{
    public static void main(String args[])
    {
           LinkedList<Integer> list = new LinkedList<>();
           list.add(5);
           list.add(1);
           list.add(2);
           list.add(1);
           list.add(9);
           list.add(1);
           System.out.println(list);
           System.out.println("Removing element 2 present at index 2");
           list.remove(2);
           System.out.println(list);
           System.out.println("Removing element 1 :  "+list.remove(1));
           System.out.println(list);
          
    }
 
}

Output :

output of remove(int  index), remove(E e)
output of remove(int index), remove(E e)

removeFirstOccurrence(E e) and removeLastOccurrence(E e): Both methods remove the first and last occurrence of an element in the list respectively.

//{ Driver Code Startspackage newPackage;
package newPackage;
import java.util.*;
import java.io.*;
import java.lang.*;

class BeeTechnical
{
    public static void main(String args[])
    {
           LinkedList<Integer> list = new LinkedList<>();
           list.add(5);
           list.add(1);
           list.add(2);
           list.add(1);
           list.add(9);
           list.add(1);
           System.out.println(list);
           System.out.println("Removing First occurrence of 1");
           list.removeFirstOccurrence(1);
           System.out.println(list);
           System.out.println("Removing Last occurrence of 1");
           list.removeLastOccurrence(1);
           System.out.println(list);         
    }
}

Output :

output of remove first occurrence() and removeLastOccurrence() methods in java linked list
output of remove first occurrence() and removeLastOccurrence() methods

Conclusion

In the article above we came to know what is Linked List its visual structure, and its types, and explore various in-built methods in java.util.LinkedList class along with codes and their output.

Comments are closed.

Scroll to Top