Java LinkedList

A LinkedList in Java is a part of the Java Collections Framework and is implemented in the java.util package. Unlike arrays or ArrayList, a LinkedList is a doubly linked list where each element (called a node) points to both the next and previous elements in the list. This provides efficient insertion and deletion at both ends of the list.

Important Features of LinkedList

Doubly Linked: Each node contains two references: one for the next node and one for the previous node.

Dynamic Size: Unlike arrays, the size of a LinkedList is dynamic, meaning it can grow or shrink as elements are added or removed.

Efficient Insertions and Deletions: Insertion and removal of elements are faster compared to arrays or ArrayList (especially at the beginning or middle of the list).

When to Use LinkedList?

1. When you need frequent insertions or deletions at the beginning or middle of a list.

2. When you don’t need random access (i.e., accessing elements by index) frequently, as accessing elements by index in a LinkedList is slower than an ArrayList.

Import the LinkedList Class

First, you need to import the LinkedList class from the java.util package.


import java.util.LinkedList;  // Import LinkedList class

Create a LinkedList

Create an instance of LinkedList by using the LinkedList class. You can specify the type of elements the list will hold (e.g., String, Integer).


// Creating a LinkedList to store Strings
LinkedList fruits = new LinkedList<>();

Here, fruits is a LinkedList that will hold String elements.

Add Elements to the LinkedList

You can add elements to the list using the add() method. You can add elements at the end of the list.


fruits.add("Apple");  // Adds "Apple" to the list
fruits.add("Banana"); // Adds "Banana" to the list
fruits.add("Mango"); // Adds "Mango" to the list
fruits.add("Orange"); // Adds "Orange" to the list

Access Elements from the LinkedList

You can retrieve an element from the list by its index using the get() method. Remember that LinkedList supports indexed access but is slower than ArrayList for this operation.


System.out.println(fruits.get(0)); // Output: Apple
System.out.println(fruits.get(1)); // Output: Banana
System.out.println(fruits.get(2)); // Output: Mango
System.out.println(fruits.get(3)); // Output: Orange

Get the Size of the LinkedList

You can get the number of elements in the list using the size() method.


System.out.println("Size of the list: " + fruits.size()); // Output: 4

Add Elements at the Beginning or End of the LinkedList


fruits.addFirst("PineApple"); // Adds "PineApple" at the beginning of the list
fruits.addLast("Grapes");  // Adds "Grapes" at the end of the list

Output:

[PineApple, Apple, Banana, Mango, Orange, Grapes]

Remove Elements from the LinkedList

You can remove elements from the list using the remove() method by specifying the index or the element itself.

  1. To remove the first element, use removeFirst().
  2. To remove the last element, use removeLast().

fruits.removeFirst();  // Removes "PineApple"
fruits.removeLast();   // Removes "Grapes"

After removing the first and last elements, the list will look like this:

[Apple, Banana, Mango, Orange]

Use remove() method to remove item from the list.


fruits.remove("Banana");  // Removes the element "Banana"

Output:

[Apple, Mango, Orange]

Check if an Element Exists in the LinkedList

You can use the contains() method to check if an element exists in the list.


System.out.println(fruits.contains("Apple"));  // Output: true
System.out.println(fruits.contains("Banana")); // Output: false

Clear All Elements in the LinkedList

If you want to remove all elements from the list, you can use the clear() method.


fruits.clear();  // Clears the entire list
System.out.println(fruits);  // Output: []

Example:


//Main.java file
import java.util.LinkedList;  // Importing LinkedList class

public class Main {
    public static void main(String[] args) {
        // Step 1: Create a LinkedList to store Strings
        LinkedList fruits = new LinkedList<>();

        // Step 2: Add elements to the LinkedList
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Mango"); 
        fruits.add("Orange");

        // Step 3: Access elements from the LinkedList
        System.out.println("First fruit: " + fruits.get(0));  // Output: Apple
        System.out.println("Second fruit: " + fruits.get(1)); // Output: Banana
        System.out.println("Third fruit: " + fruits.get(2));  // Output: Mango
        System.out.println("Fourth fruit: " + fruits.get(3));  // Output: Orange

        // Step 4: Get the size of the LinkedList
        System.out.println("Size of the list: " + fruits.size());  // Output: 4

        // Step 5: Add elements at the beginning or end
        fruits.addFirst("PineApple");
        fruits.addLast("Grapes");
        System.out.println("After adding first and last: " + fruits);  // Output: [PineApple, Apple, Banana, Mango, Orange, Grapes]

        // Step 6: Remove elements from the LinkedList
        fruits.removeFirst();  // Removes "PineApple"
        fruits.removeLast();   // Removes "Grapes"
        System.out.println("After removing first and last: " + fruits);  // Output: [Apple, Banana, Mango, Orange]

        // Remove a specific element
        fruits.remove("Banana");  // Removes "Banana"
        System.out.println("After removing Banana: " + fruits);  // Output: [Apple, Mango, Orange]

        // Step 7: Check if an element exists
        System.out.println("Contains Apple? " + fruits.contains("Apple"));  // Output: true
        System.out.println("Contains Banana? " + fruits.contains("Banana")); // Output: false

        // Step 8: Clear all elements
        fruits.clear();
        System.out.println("After clearing the list: " + fruits);  // Output: []
    }
}