LinkedList in Java
In Java programming, choosing the correct data structure significantly impacts the
performance and efficiency of applications. One such data structure is the LinkedList, which
offers efficient insertions and deletions. It is ideal for memory management, real-time
applications, and undo operations in text editors.
This guide covers the types of LinkedLists, operations, working principles, and more.
Whether you’re a beginner or an experienced Java developer, this document helps you
understand and implement LinkedLists effectively.
What is a LinkedList in Java?
A LinkedList is a linear data structure that stores elements (nodes) where each node
contains a value and a pointer to the next node. It supports dynamic memory allocation and
efficient element operations such as insertion, deletion, and traversal.
Core Concept of LinkedLists
Imagine a train where each compartment (node) is connected to the next through a
coupling (pointer). Each node stores data and a pointer to the next node. The last node
points to null, indicating the end of the list. Unlike arrays, LinkedLists require traversal from
the start to access elements.
Key Differences Between LinkedList and Array
Feature Array LinkedList
Storage Type Contiguous Non-contiguous
memory memory
allocation
Size Flexibility Fixed-size Dynamic size
Insertion/ Slow (shifting Fast (pointer
Deletion required) update)
Memory Usage Efficient Extra memory
for pointers
Access Time Fast (random Slow
access) (sequential
access)
Advantages of LinkedList Over Array
• Dynamic Size: No need to predefine size, grows/shrinks dynamically.
• Efficient Insertions and Deletions: No shifting of elements required.
• Better Memory Utilization: Memory used only for existing elements.
Working of LinkedList in Java
LinkedList differs from arrays with dynamic memory, efficient insertion/deletion, and
adaptive handling.
1. How LinkedList Stores Data
Each node contains:
1. Data – actual value
2. Pointer – reference to the next node
2. Memory Allocation
Uses non-contiguous memory with each node storing a reference to the next. Java’s garbage
collector handles unused nodes.
3. Example: Dynamic Memory Allocation
class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
public class LinkedListMemoryDemo {
public static void main(String[] args) {
Node head = new Node(10);
head.next = new Node(20);
head.next.next = new Node(30);
Node temp = head;
while (temp != null) {
System.out.println("Data: " + temp.data + ", Address: " + temp);
temp = temp.next;
}
}
}
Creating a Custom LinkedList in Java
While Java provides a built-in LinkedList, creating a custom version helps understand
internal operations.
1. Node Class
class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
2. Custom LinkedList Class
class CustomLinkedList {
Node head;
public void add(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
return;
}
Node temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = newNode;
}
public void display() {
Node temp = head;
if (temp == null) {
System.out.println("LinkedList is empty.");
return;
}
while (temp != null) {
System.out.print(temp.data + " -> ");
temp = temp.next;
}
System.out.println("NULL");
}
}
public class LinkedListDemo {
public static void main(String[] args) {
CustomLinkedList list = new CustomLinkedList();
list.add(10);
list.add(20);
list.add(30);
list.add(40);
list.display();
}
}
Types of LinkedLists in Java
Java provides multiple types of LinkedLists:
1. Singly LinkedList
Each node contains data and reference to the next node. The last node points to NULL.
1.1 Implementation
class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
public class SinglyLinkedList {
Node head;
public void add(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
return;
}
Node temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = newNode;
}
public void display() {
Node temp = head;
while (temp != null) {
System.out.print(temp.data + "-> ");
temp = temp.next;
}
System.out.println("NULL");
}
public static void main(String[] args) {
SinglyLinkedList list = new SinglyLinkedList();
list.add(10);
list.add(20);
list.add(30);
list.display();
}
}