📊 Java Data Structures
Estimated reading: 3 minutes 427 views

Java LinkedList – Complete Guide with Syntax, Examples & Use Cases


Introduction – Why Java LinkedList Is Important

Imagine you’re managing a dynamic playlist or task queue where items are frequently added or removed — an ArrayList would slow down due to shifting. This is where Java LinkedList shines.

The LinkedList class is part of Java’s Collections Framework, offering a doubly-linked list structure for efficient insertion and deletion.

By the end of this article, you’ll learn:

How Java LinkedList works and when to use it
Syntax, core methods, and real-world examples
Differences between LinkedList and ArrayList
Best practices and performance insights


What Is LinkedList in Java?

LinkedList in Java is a doubly-linked list implementation of the List, Deque, and Queue interfaces.

It allows:

  • Dynamic resizing
  • Fast insertion/deletion at both ends
  • Support for null, duplicates, and sequential access

Basic Syntax – Creating a LinkedList

import java.util.LinkedList;

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

        colors.add("Red");
        colors.add("Green");
        colors.add("Blue");

        System.out.println(colors); // Output: [Red, Green, Blue]
    }
}

LinkedList<String> declares a list of Strings
add() appends elements to the end
Insertion order is preserved


LinkedList Key Methods

MethodDescriptionExample
add(E e)Add element to endlist.add("Orange")
addFirst(E e)Insert at the beginninglist.addFirst("Mango")
addLast(E e)Insert at the endlist.addLast("Peach")
getFirst()Get first elementlist.getFirst()
getLast()Get last elementlist.getLast()
removeFirst()Remove from frontlist.removeFirst()
removeLast()Remove from endlist.removeLast()
peek()Retrieve head without removinglist.peek()
poll()Retrieve and remove headlist.poll()

Iterating Over a LinkedList

Using for-each loop

for (String color : colors) {
    System.out.println(color);
}

Using forEach() (Java 8+)

colors.forEach(System.out::println);

LinkedList as Queue and Stack

Queue Behavior (FIFO)

LinkedList<String> queue = new LinkedList<>();
queue.add("Task 1");
queue.add("Task 2");

System.out.println(queue.poll()); // Task 1

Stack Behavior (LIFO)

LinkedList<Integer> stack = new LinkedList<>();
stack.push(10);
stack.push(20);

System.out.println(stack.pop()); // 20

push()/pop() make LinkedList behave like a stack
add()/poll() behave like a queue


LinkedList vs ArrayList

FeatureLinkedListArrayList
Underlying StructureDoubly linked listDynamic array
Access time Slower (O(n)) Fast (O(1) for index)
Insert/Delete Fast at start/mid (O(1)/O(n)) Slower (due to shifting)
Memory usageMore (extra node pointers)Less
Use caseFrequent insert/deleteFrequent read/access

Real-World Example – Task Scheduler

LinkedList<String> tasks = new LinkedList<>();

tasks.add("Design UI");
tasks.add("Write API");
tasks.addFirst("Login");

System.out.println("First task: " + tasks.getFirst());

Add high-priority tasks at the front
Useful for job queues, undo stacks, playlists, etc.


Best Practices for Using LinkedList

Use when insertions/deletions are frequent
Avoid for random access-heavy operations
Use Deque interface methods (addFirst, removeLast) for stack/queue behavior
Avoid mixing ArrayList and LinkedList unless necessary


Summary

  • LinkedList is a versatile, doubly-linked structure in Java
  • Ideal for frequent insert/delete operations
  • Can function as List, Queue, or Stack
  • Offers built-in methods for both end-based and index-based manipulation

FAQs – Java LinkedList

Can LinkedList store null values?

Yes. It can store multiple null elements.

Is LinkedList synchronized?

No. Use Collections.synchronizedList() or ConcurrentLinkedDeque for thread safety.

How does LinkedList differ from ArrayList?

LinkedList offers faster insert/delete, but slower random access.

Can LinkedList be used as a Queue?

Yes. It implements the Queue and Deque interfaces.

What is the time complexity of adding/removing elements?

  • Add/remove at ends: O(1)
  • Add/remove in middle: O(n)
  • Access by index: O(n)

Share Now :
Share

Java LinkedList

Or Copy Link

CONTENTS
Scroll to Top