Java LinkedList

 The LinkedList class of the Java collections framework provides the functionality of the linked list data structure (doubly linkedlist).

Creating a Java LinkedList

Here is how we can create linked lists in Java

LinkedList<Type> linkedList = new LinkedList<>();

Example: Create LinkedList in Java

import java.util.LinkedList;

class Main {
  public static void main(String[] args){

    // create linkedlist
    LinkedList<String> animals = new LinkedList<>();

    // Add elements to LinkedList
    animals.add("Dog");
    animals.add("Cat");
    animals.add("Cow");
    System.out.println("LinkedList: " + animals);
  }
}

Output

LinkedList: [Dog, Cat, Cow]

Methods of Java LinkedList

LinkedList provides various methods that allow us to perform different operations in linked lists. We will look at four commonly used LinkedList Operators in this tutorial:

  • Add elements
  • Access elements
  • Change elements
  • Remove elements

1. Add elements to a LinkedList

We can use the add() method to add an element (node) at the end of the LinkedList. For example,

import java.util.LinkedList;

class Main {
  public static void main(String[] args){
    // create linkedlist
    LinkedList<String> animals = new LinkedList<>();

    // add() method without the index parameter
    animals.add("Dog");
    animals.add("Cat");
    animals.add("Cow");
    System.out.println("LinkedList: " + animals);

    // add() method with the index parameter
    animals.add(1, "Horse");
    System.out.println("Updated LinkedList: " + animals);
  }
}

Output

LinkedList: [Dog, Cat, Cow]
Updated LinkedList: [Dog, Horse, Cat, Cow]

2. Access LinkedList elements

The get() method of the LinkedList class is used to access an element from the LinkedList. For example,

import java.util.LinkedList;

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

    // add elements in the linked list
    languages.add("Python");
    languages.add("Java");
    languages.add("JavaScript");
    System.out.println("LinkedList: " + languages);

    // get the element from the linked list
    String str = languages.get(1);
    System.out.print("Element at index 1: " + str);
  }
}

Output

LinkedList: [Python, Java, JavaScript]
Element at index 1: Java

3. Change Elements of a LinkedList

The set() method of LinkedList class is used to change elements of the LinkedList. For example,

import java.util.LinkedList;

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

    // add elements in the linked list
    languages.add("Java");
    languages.add("Python");
    languages.add("JavaScript");
    languages.add("Java");
    System.out.println("LinkedList: " + languages);

    // change elements at index 3
    languages.set(3, "Kotlin");
    System.out.println("Updated LinkedList: " + languages);
  }
}

Output

LinkedList: [Java, Python, JavaScript, Java]
Updated LinkedList: [Java, Python, JavaScript, Kotlin]

4. Remove element from a LinkedList

The remove() method of the LinkedList class is used to remove an element from the LinkedList. For example,

import java.util.LinkedList;

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

    // add elements in LinkedList
    languages.add("Java");
    languages.add("Python");
    languages.add("JavaScript");
    languages.add("Kotlin");
    System.out.println("LinkedList: " + languages);

    // remove elements from index 1
    String str = languages.remove(1);
    System.out.println("Removed Element: " + str);

    System.out.println("Updated LinkedList: " + languages);
  }
}

Output

LinkedList: [Java, Python, JavaScript, Kotlin]
Removed Element: Python
New LinkedList: [Java, JavaScript, Kotlin]

Other Methods

MethodsDescription
contains()checks if the LinkedList contains the element
indexOf()returns the index of the first occurrence of the element
lastIndexOf()returns the index of the last occurrence of the element
clear()removes all the elements of the LinkedList
iterator()returns an iterator to iterate over LinkedList
addFirst()adds the specified element at the beginning of the linked list
addLast()adds the specified element at the end of the linked list
getFirst()returns the first element
getLast()returns the last element
removeFirst()removes the first element
removeLast()removes the last element
peek()returns the first element (head) of the linked list
poll()returns and removes the first element from the linked list
offer()adds the specified element at the end of the linked list

LinkedList Vs. ArrayList

LinkedListArrayList
Implements List, Queue, and Deque interfaces.Implements List interface.
Stores 3 values (previous address, data, and next address) in a single position.Stores a single value in a single position.
Provides the doubly-linked list implementation.Provides a resizable array implementation.
Whenever an element is added, prev and next address are changed.Whenever an element is added, all elements after that position are shifted.
To access an element, we need to iterate from the beginning to the element.Can randomly access elements using indexes.

The important points about Java LinkedList are:

  • Java LinkedList class can contain duplicate elements.
  • Java LinkedList class maintains insertion order.
  • Java LinkedList class is non synchronized.
  • In Java LinkedList class, manipulation is fast because no shifting needs to occur.
  • Java LinkedList class can be used as a list, stack or queue.