CodeToConcept
About

java

java Tutorials

Introduction
Java Variables
Java Data Types
Java Type Casting
Java Operators
Java Input/Output
Java Class & Object
java Method
Java Constructor
Java this keyword
Java super keyword
Java Method Overloading and Overriding
Java Interface
Java Abstract Class
Java Memory Management
Design Patterns
JDBC (Database Connectivity)
Java IDEs

java Tutorials

Introduction
Java Variables
Java Data Types
Java Type Casting
Java Operators
Java Input/Output
Java Class & Object
java Method
Java Constructor
Java this keyword
Java super keyword
Java Method Overloading and Overriding
Java Interface
Java Abstract Class
Java Memory Management
Design Patterns
JDBC (Database Connectivity)
Java IDEs

Sample Question

Think it through, then check the tutorial.

CodeToConcept

Quickly learn through simple, structured, and practical coding tutorials. Build your confidence from step zero to real-world experience.

© 2026 CodeToConcept. All rights reserved.

Quick Links

AboutTutorials

Tutorials

JavaHTMLCSS

Support

Privacy PolicyTerms of ServiceContact
GitHubTwitterYouTube

Java LinkedList

LinkedList is a class in the Java Collections Framework that implements both the List and Deque interfaces.

It stores elements as a sequence of linked nodes rather than using a contiguous array structure.

LinkedList<String> names = new LinkedList<>();
 
names.add("Rahul");
names.add("Priya");
names.add("Amit");
 
System.out.println(names);

Output

[Rahul, Priya, Amit]

Why Use LinkedList?

LinkedList is useful when an application needs list operations along with frequent insertion or removal at the ends or through linked-list operations.

It can also be used as:

  • A List
  • A Queue
  • A Deque

For example:

LinkedList<String> queue = new LinkedList<>();
 
queue.addLast("A");
queue.addLast("B");
queue.addLast("C");
 
System.out.println(queue);

Creating a LinkedList

import java.util.LinkedList;
 
LinkedList<String> names = new LinkedList<>();

Or program against an interface:

List<String> names = new LinkedList<>();

For queue/deque behavior:

Deque<String> names = new LinkedList<>();

Adding Elements

LinkedList<String> names = new LinkedList<>();
 
names.add("Amit");
names.add("Rahul");
names.add("Priya");

Adding at a Specific Index

names.add(1, "Sneha");

Example

Before:

[Amit, Rahul, Priya]

After:

[Amit, Sneha, Rahul, Priya]

Adding at the Beginning

names.addFirst("Ravi");

Adding at the End

names.addLast("Neha");

Accessing Elements

String name = names.get(0);

LinkedList also provides:

names.getFirst();
names.getLast();

Updating Elements

names.set(1, "Anita");

Removing Elements

Remove by object:

names.remove("Rahul");

Remove by index:

names.remove(1);

Remove first:

names.removeFirst();

Remove last:

names.removeLast();

LinkedList as a Queue

Because LinkedList implements Deque and Queue, it can be used as a queue.

Queue<String> queue = new LinkedList<>();
 
queue.offer("A");
queue.offer("B");
queue.offer("C");
 
System.out.println(queue);
 
System.out.println(queue.poll());

Output

[A, B, C]
A

The queue follows FIFO behavior: First In, First Out

LinkedList as a Deque

A LinkedList can also add and remove elements from both ends.

Deque<String> deque = new LinkedList<>();
 
deque.addFirst("A");
deque.addLast("B");
 
System.out.println(deque);

Output

[A, B]

Iterating Through LinkedList

for (String name : names) {
    System.out.println(name);
}

LinkedList Allows Duplicates

LinkedList<String> names = new LinkedList<>();
 
names.add("Amit");
names.add("Rahul");
names.add("Amit");
 
System.out.println(names);

Output

[Amit, Rahul, Amit]

LinkedList Allows Null

names.add(null);

A LinkedList can contain null values.

ArrayList vs LinkedList

FeatureArrayListLinkedList
Underlying structureResizable arrayLinked nodes
Implements ListYesYes
Implements DequeNoYes
Random/index accessEfficientGenerally slower
Add/remove at endsEnd operations are efficient; front changes shift elementsBoth ends support constant-time insertion/removal
Memory overheadLowerHigher
Queue/Deque operationsNot its primary roleSupported

Important Point About Insertion

A common oversimplification is that LinkedList insertion is always faster than ArrayList insertion.

The actual performance depends on where the operation occurs and whether the required position/node has already been located.

For example, inserting into the middle of a LinkedList still requires traversing to that position unless we already have an appropriate list iterator or node position.

Therefore, choose a collection based on the operations your application actually performs.

Example: Queue Using LinkedList

import java.util.LinkedList;
import java.util.Queue;
 
public class HelloWorld {
    public static void main(String[] args) {
 
        Queue<String> queue = new LinkedList<>();
 
        queue.offer("Customer 1");
        queue.offer("Customer 2");
        queue.offer("Customer 3");
 
        System.out.println(queue);
 
        String customer = queue.poll();
 
        System.out.println("Processing: " + customer);
        System.out.println("Remaining: " + queue);
    }
}

Output

[Customer 1, Customer 2, Customer 3]
Processing: Customer 1
Remaining: [Customer 2, Customer 3]

Common LinkedList Methods

MethodPurpose
add()Adds an element
addFirst()Adds at beginning
addLast()Adds at end
get()Gets element by index
getFirst()Gets first element
getLast()Gets last element
set()Updates an element
remove()Removes an element
removeFirst()Removes first element
removeLast()Removes last element
offer()Adds as queue operation
poll()Removes head
peek()Reads head

When Should You Use LinkedList? Consider LinkedList when: You need List and Deque behavior. You frequently operate at the beginning or end. You need queue/deque methods directly from the implementation. For many ordinary list use cases where index-based access is common, ArrayList is generally more suitable.

Key Takeaway

LinkedList is a linked-node implementation that supports both List and Deque operations. It is particularly useful when you need operations at both ends or want a list implementation that can also behave as a queue or deque.

See you in next page....🙂

← ArrayListVector →
← ArrayListVector →