WikiGalaxy

Personalize

Introduction to Java LinkedList

Overview:

The Java LinkedList class is a part of the Java Collections Framework and implements the List, Queue, and Deque interfaces. It is used for storing elements in a linked list structure, allowing for efficient insertion and removal operations.

Key Features:

LinkedList allows null elements, maintains insertion order, and is not synchronized. It provides constant-time performance for add and remove operations.

Creating a LinkedList

Example:

Let's create a LinkedList of strings and add some elements to it.


import java.util.LinkedList;

class Example {
  public static void main(String args[]) {
      LinkedList list = new LinkedList();
      list.add("Apple");
      list.add("Banana");
      list.add("Cherry");
      System.out.println(list);
  }
}
      

Console Output:

[Apple, Banana, Cherry]

Accessing Elements

Example:

Access elements using the get() method.


import java.util.LinkedList;

class Example {
  public static void main(String args[]) {
      LinkedList list = new LinkedList();
      list.add("Apple");
      list.add("Banana");
      list.add("Cherry");
      System.out.println("First element: " + list.get(0));
  }
}
      

Console Output:

First element: Apple

Removing Elements

Example:

Remove elements using the remove() method.


import java.util.LinkedList;

class Example {
  public static void main(String args[]) {
      LinkedList list = new LinkedList();
      list.add("Apple");
      list.add("Banana");
      list.add("Cherry");
      list.remove("Banana");
      System.out.println(list);
  }
}
      

Console Output:

[Apple, Cherry]

Iterating through LinkedList

Example:

Use an iterator to traverse the elements.


import java.util.LinkedList;
import java.util.Iterator;

class Example {
  public static void main(String args[]) {
      LinkedList list = new LinkedList();
      list.add("Apple");
      list.add("Banana");
      list.add("Cherry");
      Iterator iterator = list.iterator();
      while (iterator.hasNext()) {
          System.out.println(iterator.next());
      }
  }
}
      

Console Output:

Apple
Banana
Cherry

Using LinkedList as a Queue

Example:

Implement queue operations using LinkedList.


import java.util.LinkedList;
import java.util.Queue;

class Example {
  public static void main(String args[]) {
      Queue queue = new LinkedList();
      queue.add("Apple");
      queue.add("Banana");
      queue.add("Cherry");
      System.out.println("Queue: " + queue);
      System.out.println("Removed: " + queue.remove());
      System.out.println("Queue after removal: " + queue);
  }
}
      

Console Output:

Queue: [Apple, Banana, Cherry]
Removed: Apple
Queue after removal: [Banana, Cherry]

Using LinkedList as a Deque

Example:

Perform deque operations like adding and removing elements from both ends.


import java.util.LinkedList;
import java.util.Deque;

class Example {
  public static void main(String args[]) {
      Deque deque = new LinkedList();
      deque.addFirst("Apple");
      deque.addLast("Banana");
      deque.addFirst("Cherry");
      System.out.println("Deque: " + deque);
      deque.removeFirst();
      System.out.println("Deque after removal: " + deque);
  }
}
      

Console Output:

Deque: [Cherry, Apple, Banana]
Deque after removal: [Apple, Banana]

Checking LinkedList Size

Example:

Use the size() method to determine the number of elements in the LinkedList.


import java.util.LinkedList;

class Example {
  public static void main(String args[]) {
      LinkedList list = new LinkedList();
      list.add("Apple");
      list.add("Banana");
      list.add("Cherry");
      System.out.println("Size of the LinkedList: " + list.size());
  }
}
      

Console Output:

Size of the LinkedList: 3

Clearing a LinkedList

Example:

Use the clear() method to remove all elements from the LinkedList.


import java.util.LinkedList;

class Example {
  public static void main(String args[]) {
      LinkedList list = new LinkedList();
      list.add("Apple");
      list.add("Banana");
      list.add("Cherry");
      list.clear();
      System.out.println("LinkedList after clear operation: " + list);
  }
}
      

Console Output:

LinkedList after clear operation: []

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025