🎉 Festival Deal: Save Big on Professional Courses with Real Projects – Apply Coupons FEST300OFF FEST500OFF
→ Click Here ←

LinkedList Class in Java  


Introduction
  • LinkedList is an implemented class of the List and Deque interfaces.
  • It is present in the java.util package.
  • It was introduced in JDK 1.2 version.
  • Hierarchy:
    • LinkedList Hierarchy in Java
  • Syntax :-
    package java.util;
    
    public class LinkedList<E> implements List<E>, Deque<E>, Cloneable, java.io.Serializable
    {    
        // Constructors
        
        // Methods
        
        // Fields
    }
    • LinkedList implements both List and Deque interfaces, so it can be used as a list, queue, or stack.
    • It does not implement RandomAccess, so accessing elements by index is slower compared to ArrayList.
  • LinkedList is a doubly-linked list data structure that allows efficient insertion and deletion of elements.
    LinkedList in Java
Constructors of LinkedList Class
  • Below are the constructors defined in the LinkedList class:
Sr. No. Constructor Description
1 LinkedList() Constructs an empty LinkedList.
2 LinkedList(Collection<? extends E> c) Constructs a LinkedList containing the elements of the specified collection, in the order returned by the collection’s iterator.
Methods of LinkedList Class
  • Below are some methods defined specifically in the LinkedList class:
Sr. No. Method Description
1 void addFirst(E e) Inserts the specified element at the beginning of the LinkedList.
2 void addLast(E e) Appends the specified element to the end of the LinkedList.
3 E getFirst() Returns the first element in the LinkedList.
4 E getLast() Returns the last element in the LinkedList.
5 E removeFirst() Removes and returns the first element from the LinkedList.
6 E removeLast() Removes and returns the last element from the LinkedList.
7 void push(E e) Pushes an element onto the stack represented by this LinkedList.
8 E pop() Pops an element from the stack represented by this LinkedList.
9 boolean offer(E e) Inserts the specified element at the end of the queue.
10 E poll() Retrieves and removes the head (first element) of the queue, or returns null if the queue is empty.
Program :
  • In below program, we are directly using LinkedList class.
  • import java.util.LinkedList;
    
    public class LinkedListDemo
    {
        public static void main(String[] args)
        {
            LinkedList<String> list = new LinkedList<>();
    
            // Adding elements
            list.add("Apple");
            list.add("Banana");
            list.add("Mango");
            list.add("Banana"); // duplicate allowed
    
            System.out.println(list);
    
            System.out.println("-------------------------");
    
            // Accessing first and last elements
            System.out.println("First Element: " + list.getFirst());
            System.out.println("Last Element: " + list.getLast());
    
            System.out.println("-------------------------");
    
            // Updating element
            list.set(1, "Orange");
            System.out.println(list);
    
            System.out.println("-------------------------");
    
            // Removing element
            list.remove("Apple");
            System.out.println(list);
    
            System.out.println("-------------------------");
    
            // Using LinkedList as a Queue
            list.addFirst("Grapes");
            list.addLast("Pineapple");
            System.out.println(list);
    
            System.out.println("-------------------------");
    
            // Iterating LinkedList
            for(String fruit : list)
            {
                System.out.println(fruit);
            }
        }
    }
Properties of LinkedList Class:
  1. LinkedList is a linear data structure that stores elements in nodes, where each node contains data and references (links) to the next and previous node.
  2. LinkedList implements both the List and Deque interfaces, so it can be used as a list, queue, or stack.
  3. LinkedList can store heterogeneous elements if generics are not used.
  4. It allows storing of duplicate elements.
  5. It allows storing of null values.
  6. Insertion order is preserved, meaning elements are retrieved in the same sequence they were added.
  7. Sorting order is not maintained; explicit sorting is required using Collections.sort() or list.sort().
  8. LinkedList is non-synchronized and not thread-safe.
  9. Like ArrayList, it does not guarantee data consistency in multi-threaded environments.