๐Ÿ“Š Java Data Structures
Estimated reading: 3 minutes 32 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Java LinkedList

Or Copy Link

CONTENTS
Scroll to Top