Similar to Arrays in java Linked List is also a Linear data structure. In Linked List unlike arrays elements are not stored in a contiguous locations and they are linked to each other using references(pointers in memory), in Linked List each element has a reference pointing it to the next element in the Linked List.

Elements in the Linked List are called node, each node in the Linked List contains two items

  1.  Node(content of the element)
  2. Reference Address(pointer to the next node in the linked list)

LinkedList Implementation in Java

The Java LinkedList class provides a doubly linked list implementation.

Each element in a singly linked list is known as a node. It consists of 2 fields:

  • Next – Stores an address of the next element in the list. It is null for the last element.
  • Data – Stores the actual data.

Elements in linked lists are not stored in sequence. Instead, they are scattered and connected through links ( Next),

Another node called ‘Previous’ node is present in doubly linked list, which links the previous elements in the list.

How to create a linkedList

Here is how we can create linked lists in Java:

LinkedList<Type> linkedList = new LinkedList<>();

Here, Type indicates the type of a linked list. For example,

// create String type linked list
LinkedList<String> linkedList = new LinkedList<>();

// create Integer type linked list
LinkedList<Interger> linkedList = new LinkedList<>();

LinkedList using Interfaces

Let’s take an example.

List<String> food = new LinkedList<>();

Here we have declared a linked list, food, using the List interface. The linked list can only access the methods of the List interface.

Methods of LinkedList

LinkedList provides various methods that allow us to perform different operations in linked lists.

Add Elements to LinkedList

1. Add Elements: using the add() method

To add an element (node) to the end of the linked list, we use the add() method. For example,

Code:

import java.util.LinkedList;

class Main {
    public static void main(String[] args){
        LinkedList<String> food= new LinkedList<>();

        // Add elements to LinkedList
        food.add("Apple");
        food.add("Mango");
        food.add("Banana");
        System.out.println("Food List: " + food);
    }
}

Output

Food List: [Apple, Mango, Banana] 

Accessing LinkedList Elements

1. Using get() Method :

To access an element from the linked list, we can use the get() method. For example,

Code:

import java.util.LinkedList;

class Main {
    public static void main(String[] args) {
        LinkedList<String> food= new LinkedList<>();

        // Add elements in the linked list
        food.add("Apple"); food.add("Mango"); food.add("Banana"); System.out.println("Food List: " + food); // Get the element from the linked list String foodName = food.get(2); System.out.print("Element at index 1: " + foodName ); } }

Output

Food List: [Apple, Mango, Banana]
Element at index 2: Banana

1. Using the contains() Method: 

To check if a linked list contains a particular element or not, we use the contains() method. For example,

Code:

import java.util.LinkedList;

class Main {
    public static void main(String[] args) {
        LinkedList<String> food= new LinkedList<>();

        // Add elements in the linked list
        food.add("Apple");
        food.add("Mango");
        food.add("Banana");
        System.out.println("Food List: " + food);

        // Checks if Mango is in the food list
        if(food.contains("Mango")) {
            System.out.println("Mango is in Food List.");
        }
    }
}

Output

Food List: [Apple, Mango, Banana]
Mango is in Food List.

Changing LinkedList Elements

1. Using the set() Method: 

To change elements of a linked list, we can use the set() method. For example,

Code:

// Change elements at index 2 food.set(2, "Kiwi");
System.out.println("New Food List: " + Kiwi);

 

Output

Food List: [Apple, Mango, Banana]
New Food List: [Apple, Mango, Kiwi]

Removing LinkedList Elements

1. Using remove() Method:

To remove an element from the linked list, we can use the remove() method. For example,

Code:


// Remove elements from index 1
String foodName= food.remove(1);
System.out.println("Removed Element: " + foodName);

Output

Food List: [Apple, Mango, Kiwi]
Removed Element: Mango
New Food List: [Apple, Kiwi]

Also Read : Linked List