Java - LinkedList

Java LinkedList is a data structure that allows us to store and manipulate a sequence of elements. It is implemented using a doubly linked list, where each element is stored in a node that contains a reference to the previous and next nodes in the list. LinkedList is a part of the Java Collections framework and provides several methods to add, remove and manipulate elements.

To use LinkedList in a Java program, we need to import the java.util.LinkedList class. Here's an example of how to create a LinkedList and add elements to it:

import java.util.LinkedList;

public class LinkedListExample {
    public static void main(String[] args) {
        // Creating a LinkedList of strings
        LinkedList<String> names = new LinkedList<>();

        // Adding elements to the LinkedList
        names.add("Alice");
        names.add("Bob");
        names.add("Charlie");

        // Accessing elements in the LinkedList
        String first = names.getFirst();
        String last = names.getLast();
        String second = names.get(1);

        // Removing elements from the LinkedList
        names.removeFirst();
        names.removeLast();
        names.remove("Bob");

        // Iterating over the elements in the LinkedList
        for (String name : names) {
            System.out.println(name);
        }
    }
}

In this example, we create a LinkedList of strings and add three elements to it. We then access elements using the getFirst, getLast, and get methods, and remove elements using the removeFirst, removeLast, and remove methods. Finally, we iterate over the elements in the list using a for loop.

LinkedList provides several other methods for adding, removing and manipulating elements, such as addFirst, addLast, offer, offerFirst, offerLast, poll, pollFirst, pollLast, peek, peekFirst, peekLast, size, clear, and more. It also provides methods to convert the list to an array and to sort the elements in the list.

LinkedList is useful when we need to add or remove elements from the beginning or end of the list frequently, as these operations are very fast with LinkedList. However, accessing elements in the middle of the list is slower than with ArrayList, as LinkedList has to traverse the list from the beginning or end to reach the desired element.

import java.util.LinkedList;

public class LinkedListExample {
    public static void main(String[] args) {
        // Creating a LinkedList
        LinkedList<String> animals = new LinkedList<>();

        // Adding elements to the LinkedList
        animals.add("Lion");
        animals.add("Tiger");
        animals.add("Elephant");
        animals.add("Giraffe");

        // Displaying the LinkedList
        System.out.println("Animals: " + animals);

        // Adding an element at the first position
        animals.addFirst("Leopard");

       // Displaying the LinkedList after adding an element at the first position
        System.out.println("Animals after adding an element at the first position: " + animals);

        // Removing an element from the LinkedList
        animals.remove(2);

        // Displaying the LinkedList after removing an element
        System.out.println("Animals after removing an element: " + animals);

        // Getting the size of the LinkedList
        System.out.println("Size of the LinkedList: " + animals.size());
    }
}

In this example, we are creating a LinkedList of type String and adding some elements to it using the add() method. We are then displaying the LinkedList using the println() method.

We are adding a new element to the first position of the LinkedList using the addFirst() method, and then displaying the LinkedList again.

We are removing an element from the LinkedList using the remove() method, and then displaying the LinkedList again.

Finally, we are getting the size of the LinkedList using the size() method and displaying it.