Skip to content

Java LinkedList

Imagine you're on a quest to organize a treasure trove of gems. Each gem is precious and unique, and you need a way to store them in a flexible and efficient manner. That's where LinkedLists come into play! Picture them as a string of pearls, each one connected to the next, forming a chain of elements that can be easily manipulated and traversed.

So, what exactly is a LinkedList, you ask? Well, think of it as a linear collection of elements where each element, called a node, is connected to its successor by a reference. Unlike arrays, LinkedLists don't have a fixed size, allowing you to add, remove, and modify elements dynamically.

Some key characteristics of LinkedLists

  1. Dynamic Size: Unlike arrays, LinkedLists can grow or shrink dynamically as elements are added or removed. It's like having a magical backpack that expands to accommodate whatever treasures you find along your journey.

  2. Efficient Insertions and Deletions: LinkedLists excel at inserting and deleting elements in the middle of the list. With just a flick of your wand, you can rearrange the elements without disturbing the rest of the collection.

  3. Traversal: Like following a trail of breadcrumbs through the forest, LinkedLists allow you to traverse your data in a sequential manner. Each element points to the next one, leading you on a delightful journey through your collection.

  4. Versatility: LinkedLists can store any type of object, making them a versatile choice for a wide range of applications. Whether you're storing integers, strings, or even custom objects, LinkedLists have you covered.

Examples

java
import java.util.LinkedList;

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

        // Adding elements to the LinkedList
        linkedList.add("Apple");
        linkedList.add("Banana");
        linkedList.add("Cherry");

        // Displaying the elements of the LinkedList
        System.out.println("LinkedList elements: " + linkedList);

        // Adding an element at the beginning
        linkedList.addFirst("Grapes");

        // Adding an element at the end
        linkedList.addLast("Watermelon");

        // Displaying the modified LinkedList
        System.out.println("Modified LinkedList: " + linkedList);
    }
}

In this example, we create a LinkedList of strings and add some fruits to it. We then demonstrate how to add elements at the beginning and end of the list. It's like building a fruit salad, where you can easily add new fruits to the mix at any point.

But wait, there's more to LinkedLists than just adding elements! They also support various operations like removing, searching, and traversing elements with ease. Plus, LinkedLists are great for scenarios where you need to frequently add or remove elements, thanks to their dynamic nature.

Let's continue our exploration with another example:

java
import java.util.LinkedList;

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

        // Adding elements to the LinkedList
        linkedList.add("Apple");
        linkedList.add("Banana");
        linkedList.add("Cherry");

        // Removing the first and last elements
        linkedList.removeFirst();
        linkedList.removeLast();

        // Displaying the updated LinkedList
        System.out.println("Updated LinkedList: " + linkedList);

        // Searching for an element
        if (linkedList.contains("Banana")) {
            System.out.println("Banana found!");
        } else {
            System.out.println("Banana not found!");
        }
    }
}

In this example, we remove the first and last elements from the LinkedList and then search for the presence of a specific element. It's like managing a guest list, where you can easily remove guests who leave early and check if someone's still on the list.

Waytojava is designed to make learning easier. We simplify examples for better understanding. We regularly check tutorials, references, and examples to correct errors, but it's important to remember that humans can make mistakes.