codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
☕

Java Programming

52 / 65 topics
50Java Collections51Java ArrayList52Java LinkedList53Java HashSet54Java HashMap55Java Iterator
Tutorials/Java Programming/Java LinkedList
☕Java Programming

Java LinkedList

Updated 2026-05-12
30 min read

Java LinkedList

In the world of programming, data structures are fundamental building blocks that help organize and manage collections of data efficiently. In this tutorial, we will dive into one such essential data structure in Java—LinkedList. We'll explore its features, differences from ArrayList, and how to use it effectively for various operations like Queue and Deque methods.

Introduction

A LinkedList is a part of the Java Collections Framework and implements the List interface. Unlike an ArrayList, which stores elements in a contiguous block of memory, a LinkedList stores its elements as nodes where each node points to the next node in the sequence. This structure allows for efficient insertion and deletion operations but can be slower for random access compared to ArrayList.

Understanding when to use a LinkedList instead of an ArrayList is crucial for optimizing your Java applications. In this tutorial, we will cover:

  • The basic structure and methods of LinkedList.
  • How LinkedList differs from ArrayList.
  • Implementing Queue and Deque operations using LinkedList.

Core Content

Basic Structure and Methods

A LinkedList in Java consists of nodes where each node contains data and a reference (link) to the next node. Here is a simple example of how you can create and manipulate a LinkedList:

BasicLinkedList.java
1import java.util.LinkedList;
2
3public class BasicLinkedList {
4 public static void main(String[] args) {
5 LinkedList<String> linkedList = new LinkedList<>();
6
7 // Adding elements
8 linkedList.add("Apple");
9 linkedList.add("Banana");
10 linkedList.add("Cherry");
11
12 // Displaying elements
13 System.out.println(linkedList);
14
15 // Accessing elements by index
16 System.out.println("First element: " + linkedList.get(0));
17
18 // Removing an element
19 linkedList.remove("Banana");
20 System.out.println("After removing Banana: " + linkedList);
21 };
22}
Output
[Apple, Banana, Cherry]
First element: Apple
After removing Banana: [Apple, Cherry]

LinkedList vs ArrayList

Both LinkedList and ArrayList are implementations of the List interface in Java. However, they have different internal structures and performance characteristics:

FeatureLinkedListArrayList
StructureDoubly linked list where each node contains a reference to the next and previous nodes.Array-based data structure that stores elements in contiguous memory locations.
Insertion/DeletionEfficient (constant time O(1) for add/remove at the head/tail).Less efficient (linear time O(n) for add/remove at any position other than the end).
Random AccessInefficient (linear time O(n)).Efficient (constant time O(1)).
Memory UsageHigher due to additional references in each node.Lower, as it only requires memory for the array itself.

Tip

Use LinkedList when you need frequent insertions and deletions, especially at the beginning or end of the list. Use ArrayList when you require fast random access.

Queue Methods

A Queue is a collection that follows the First-In-First-Out (FIFO) principle. Java provides several methods to implement queue operations using LinkedList. Here are some common queue methods:

QueueMethods.java
1import java.util.LinkedList;
2import java.util.Queue;
3
4public class QueueMethods {
5 public static void main(String[] args) {
6 Queue<String> queue = new LinkedList<>();
7
8 // Adding elements to the queue
9 queue.add("Apple");
10 queue.add("Banana");
11 queue.add("Cherry");
12
13 // Displaying the queue
14 System.out.println("Queue: " + queue);
15
16 // Removing and retrieving the head of the queue
17 String removedElement = queue.remove();
18 System.out.println("Removed element: " + removedElement);
19 System.out.println("Queue after removal: " + queue);
20
21 // Peeking at the head of the queue without removing it
22 String peekedElement = queue.peek();
23 System.out.println("Peeked element: " + peekedElement);
24 System.out.println("Queue after peek: " + queue);
25 }
26}
Output
Queue: [Apple, Banana, Cherry]
Removed element: Apple
Queue after removal: [Banana, Cherry]
Peeked element: Banana
Queue after peek: [Banana, Cherry]

Deque Methods

A Deque (double-ended queue) is a collection that allows insertion and removal of elements from both ends. Java provides several methods to implement deque operations using LinkedList. Here are some common deque methods:

DequeMethods.java
1import java.util.LinkedList;
2import java.util.Deque;
3
4public class DequeMethods {
5 public static void main(String[] args) {
6 Deque<String> deque = new LinkedList<>();
7
8 // Adding elements to the deque
9 deque.addFirst("Apple");
10 deque.addLast("Banana");
11 deque.addFirst("Cherry");
12
13 // Displaying the deque
14 System.out.println("Deque: " + deque);
15
16 // Removing and retrieving the first element of the deque
17 String removedFirstElement = deque.removeFirst();
18 System.out.println("Removed first element: " + removedFirstElement);
19 System.out.println("Deque after removal: " + deque);
20
21 // Removing and retrieving the last element of the deque
22 String removedLastElement = deque.removeLast();
23 System.out.println("Removed last element: " + removedLastElement);
24 System.out.println("Deque after removal: " + deque);
25 }
26}
Output
Deque: [Cherry, Apple, Banana]
Removed first element: Cherry
Deque after removal: [Apple, Banana]
Removed last element: Banana
Deque after removal: [Apple]

Practical Example

Let's create a practical example that demonstrates the use of LinkedList for both queue and deque operations. We'll simulate a simple task scheduler where tasks are added to a queue and processed in FIFO order, and also allow tasks to be added or removed from either end.

TaskScheduler.java
1import java.util.LinkedList;
2import java.util.Queue;
3
4public class TaskScheduler {
5 public static void main(String[] args) {
6 Queue<String> taskQueue = new LinkedList<>();
7
8 // Adding tasks to the queue
9 taskQueue.add("Task1");
10 taskQueue.add("Task2");
11 taskQueue.add("Task3");
12
13 System.out.println("Initial Task Queue: " + taskQueue);
14
15 // Processing tasks in FIFO order
16 while (!taskQueue.isEmpty()) {
17 String task = taskQueue.remove();
18 System.out.println("Processing: " + task);
19 }
20 }
21}
Output
Initial Task Queue: [Task1, Task2, Task3]
Processing: Task1
Processing: Task2
Processing: Task3

Summary

In this tutorial, we have explored the LinkedList data structure in Java. We learned about its basic methods, differences with ArrayList, and how to use it for queue and deque operations. Here are the key takeaways:

  • Structure: LinkedList is a doubly linked list that allows efficient insertion and deletion.
  • Use Cases: Use LinkedList when frequent insertions and deletions are required, especially at the beginning or end of the list.
  • Queue Methods: Implement queue operations like add, remove, and peek using LinkedList.
  • Deque Methods: Implement deque operations to allow adding/removing elements from both ends.

By understanding these concepts, you can effectively use LinkedList in your Java applications to manage collections of data efficiently.

What's Next?

Now that you have a solid understanding of LinkedList, the next topic is "Java HashSet." In this tutorial, we will explore another essential collection type—HashSet—and learn how it differs from other collections like ArrayList and LinkedList.


PreviousJava ArrayListNext Java HashSet

Recommended Gear

Java ArrayListJava HashSet