LinkedList, Java

LinkedList, Java

Overview of LinkedList in Java

A `LinkedList` is a linear data structure that consists of a sequence of elements or nodes, where each node contains data and a reference (or pointer) to the next node, previous node or both in the sequence. Unlike arrays, linked lists do not require contiguous memory allocation, it’s node can be scattered in memory anywhere, still connected, allowing for dynamic memory usage. In Java, theLinkedListclass is part of the Java Collections Framework and implements both theListandDequeinterfaces, making it a versatile choice for various applications.

Characteristics of LinkedList

  • Dynamic Size: The size of a linked list can grow or shrink dynamically as elements are added or removed.

  • Non-contiguous Memory Allocation: Nodes are stored in non-contiguous memory locations, which allows for efficient insertion and deletion.

  • Sequential Access: Elements must be accessed sequentially from the head to the tail, unlike arrays which allow random access.

  • Multi directional Traversal: In doubly LinkedList, we can traverse it in forward and backward directions.

Usage of LinkedList

LinkedList is particularly useful in scenarios where frequent insertions and deletions occur. Common use cases include:

  • Implementing stacks and queues.

  • Maintaining a list of items where order matters (e.g., playlists).

  • Managing dynamic collections where the size is unpredictable.

Advantages of LinkedList

  • Efficient Insertions/Deletions: Adding or removing elements does not require shifting other elements, making these operations O(1) if performed at the head or tail.

  • Flexible Size: Unlike arrays, linked lists can easily grow or shrink in size without needing to allocate new memory blocks.

  • Memory Efficiency: They can use memory more efficiently when dealing with varying sizes of data.

Disadvantages of LinkedList

  • Memory Overhead: Each node requires additional memory for storing pointers, which can lead to higher memory usage compared to arrays.

  • Sequential Access Only: Accessing elements requires traversal from the head, resulting in O(n) time complexity for searches.

  • Cache Performance: Due to non-contiguous storage, linked lists may perform poorly in terms of cache locality compared to arrays.

Time Complexities

OperationTime Complexity
AccessO(n)
SearchO(n)
Insertion (head/tail)O(1)
Insertion (middle)O(n)
Deletion (head/tail)O(1)
Deletion (middle)O(n)

Code Examples

Creating a LinkedList

To create a LinkedList in Java, you can use the following syntax:

javaimport java.util.LinkedList;

public class Main {
    public static void main(String[] args) {
        LinkedList<String> names = new LinkedList<>();
        names.add("Luna");
        System.out.println(names);
    }
}

Adding Elements

You can add elements to a LinkedList using various methods:

names.add("Arpit"); // Adds Arpit at the end
names.addFirst("Splendor"); // Adds Splendor at the beginning
names.add(1, "Eve"); // Adds Eve at index 1
System.out.println(names); // print the content of list

Deleting Elements

To remove elements from a LinkedList:

javanames.remove("Arpit"); // Removes Arpit
names.removeFirst(); // Removes first element
names.removeLast(); // Removes last element 
System.out.println(names); // print the list after modification

Traversing a LinkedList

You can traverse through a LinkedList using an iterator:

for (String name : names) {
    System.out.println(name);
}

Conclusion

In summary, LinkedList is a powerful and flexible data structure that is ideal for applications requiring frequent insertions and deletions. While it offers advantages such as dynamic sizing and efficient operations at both ends of the list, it also comes with trade-offs like increased memory usage and slower access times. Understanding these characteristics allows developers to make informed decisions about when to use linked lists versus other data structures like arrays or ArrayLists.

Did you find this article valuable?

Support Data Structures & algorithms by becoming a sponsor. Any amount is appreciated!