Java LinkedList
Last Updated : 26 Mar 2026
Java LinkedList is used to store and manage a collection of data where frequent insertion and deletion of elements are required.
In Java, arrays have a fixed size and are not efficient for adding or removing elements. To solve this problem, LinkedList provides a dynamic data structure where elements can be easily inserted or deleted without shifting other elements.
What is Java LinkedList?
Java LinkedList is a class that implements a doubly linked list data structure. It is part of the Java Collections Framework and is used to store elements dynamically.
Unlike arrays, LinkedList does not store elements in continuous memory. Instead, it stores elements as nodes, where each node contains:
- The actual data
- A reference to the next node
- A reference to the previous node
This makes insertion and deletion operations faster and more efficient.
The LinkedList class:
- Extends the AbstractList class
- Implements the List and Deque interfaces
LinkedList can be used in multiple ways such as:
- List
- Stack
- Queue
It also allows duplicate elements, maintains insertion order, and can grow or shrink dynamically based on the data.
LinkedList Class Declaration
LinkedList is a class that maintains insertion order and allows duplicate elements. It provides methods to add, remove, and access elements in the list.
It also implements the Deque interface, which means it supports operations at both ends (beginning and end) like adding or removing elements.
Let's see the declaration of the java.util.LinkedList class:
Constructors of Java LinkedList
The LinkedList class provides constructors to create linked list objects in different ways.
- You can create an empty LinkedList
- You can create a LinkedList from another collection
1. LinkedList()
This constructor is used to create an empty LinkedList.
Here is the syntax:
2. LinkedList(Collection c)
This constructor is used to create a LinkedList containing elements from another collection. The elements are added in the same order as the collection.
Here is the syntax:
Methods of LinkedList Class
The following table shows the commonly used methods of the LinkedList class:
| Method | Description |
|---|---|
| boolean add(E e) | It is used to append the specified element to the end of a list. |
| void add(int index, E element) | It is used to insert the specified element at the specified position index in a list. |
| boolean addAll(Collection<? extends E> c) | It is used to append all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator. |
| boolean addAll(Collection<? extends E> c) | It is used to append all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator. |
| boolean addAll(int index, Collection<? extends E> c) | It is used to append all the elements in the specified collection, starting at the specified position of the list. |
| void addFirst(E e) | It is used to insert the given element at the beginning of a list. |
| void addLast(E e) | It is used to append the given element to the end of a list. |
| void clear() | It is used to remove all the elements from a list. |
| Object clone() | It is used to return a shallow copy of an ArrayList. |
| boolean contains(Object o) | It is used to return true if a list contains a specified element. |
| Iterator<E> descendingIterator() | It is used to return an iterator over the elements in a deque in reverse sequential order. |
| E element() | It is used to retrieve the first element of a list. |
| E get(int index) | It is used to return the element at the specified position in a list. |
| E getFirst() | It is used to return the first element in a list. |
| E getLast() | It is used to return the last element in a list. |
| int indexOf(Object o) | It is used to return the index in a list of the first occurrence of the specified element, or -1 if the list does not contain any element. |
| int lastIndexOf(Object o) | It is used to return the index in a list of the last occurrence of the specified element, or -1 if the list does not contain any element. |
| ListIterator<E> listIterator(int index) | It is used to return a list-iterator of the elements in proper sequence, starting at the specified position in the list. |
| boolean offer(E e) | It adds the specified element as the last element of a list. |
| boolean offerFirst(E e) | It inserts the specified element at the front of a list. |
| boolean offerLast(E e) | It inserts the specified element at the end of a list. |
| E peek() | It retrieves the first element of a list |
| E peekFirst() | It retrieves the first element of a list or returns null if a list is empty. |
| E peekLast() | It retrieves the last element of a list or returns null if a list is empty. |
| E poll() | It retrieves and removes the first element of a list. |
| E pollFirst() | It retrieves and removes the first element of a list, or returns null if a list is empty. |
| E pollLast() | It retrieves and removes the last element of a list, or returns null if a list is empty. |
| E pop() | It pops an element from the stack represented by a list. |
| void push(E e) | It pushes an element onto the stack represented by a list. |
| E remove() | It is used to retrieve and removes the first element of a list. |
| E remove(int index) | It is used to remove the element at the specified position in a list. |
| boolean remove(Object o) | It is used to remove the first occurrence of the specified element in a list. |
| E removeFirst() | It removes and returns the first element from a list. |
| boolean removeFirstOccurrence(Object o) | It is used to remove the first occurrence of the specified element in a list (when traversing the list from head to tail). |
| E removeLast() | It removes and returns the last element from a list. |
| boolean removeLastOccurrence(Object o) | It removes the last occurrence of the specified element in a list (when traversing the list from head to tail). |
| E set(int index, E element) | It replaces the element at the specified position in a list with the specified element. |
| Object[] toArray() | It is used to return an array containing all the elements in a list in proper sequence (from first to the last element). |
| <T> T[] toArray(T[] a) | It returns an array containing all the elements in the proper sequence (from first to the last element); the runtime type of the returned array is that of the specified array. |
| int size() | It is used to return the number of elements in a list. |
Traversing LinkedList Elements
You can create a LinkedList and traverse its elements using an iterator.
Example
Let's see a simple example to traverse LinkedList elements.
Output:
Adding Elements to LinkedList
You can add elements to a LinkedList by using different ways like add(E e), add(int index, E element), addAll(Collection c), addAll(int index, Collection c), addFirst(E e), and addLast(E e) methods.
Example
Let's see a simple example to add elements in different ways.
Output:
Initial list of elements: [] After invoking add(E e) method: [Ravi, Vijay, Ajay] After invoking add(int index, E element) method: [Ravi, Gaurav, Vijay, Ajay] After invoking addAll(Collection c) method: [Ravi, Gaurav, Vijay, Ajay, Sonoo, Hanumat] After invoking addAll(int index, Collection c) method: [Ravi, John, Rahul, Gaurav, Vijay, Ajay, Sonoo, Hanumat] After invoking addFirst(E e) method: [Lokesh, Ravi, John, Rahul, Gaurav, Vijay, Ajay, Sonoo, Hanumat] After invoking addLast(E e) method: [Lokesh, Ravi, John, Rahul, Gaurav, Vijay, Ajay, Sonoo, Hanumat, Harsh]
Removing Elements from LinkedList
The LinkedList elements can be removed by using different methods like remove(Object o), remove(int index), removeAll(Collection c), removeFirst(), removeLast(), removeFirstOccurrence(Object o), removeLastOccurrence(Object o), and clear().
Example
Let's see a simple example to remove elements.
Output:
Initial list of elements: [Ravi, Vijay, Ajay, Anuj, Gaurav, Harsh, Virat, Gaurav, Harsh, Amit] After invoking remove(object) method: [Ravi, Ajay, Anuj, Gaurav, Harsh, Virat, Gaurav, Harsh, Amit] After invoking remove(index) method: [Ajay, Anuj, Gaurav, Harsh, Virat, Gaurav, Harsh, Amit] Updated list : [Ajay, Anuj, Gaurav, Harsh, Virat, Gaurav, Harsh, Amit, Ravi, Hanumat] After invoking removeAll() method: [Ajay, Anuj, Gaurav, Harsh, Virat, Gaurav, Harsh, Amit] After invoking removeFirst() method: [Gaurav, Harsh, Virat, Gaurav, Harsh, Amit] After invoking removeLast() method: [Gaurav, Harsh, Virat, Gaurav, Harsh] After invoking removeFirstOccurrence() method: [Harsh, Virat, Gaurav, Harsh] After invoking removeLastOccurrence() method: [Harsh, Virat, Gaurav] After invoking clear() method: []
Reversing LinkedList Elements
You can traverse the LinkedList in reverse order using a descending iterator.
Example
Let's see a simple example to reverse elements.
Output:
LinkedList Example with Objects (Book)
In this example, we store and traverse custom objects using LinkedList.
Let's see a simple example using a Book class.
Output:
101 Let us C Yashwant Kanetkar BPB 8 102 Data Communications & Networking Forouzan Mc Graw Hill 4 103 Operating System Galvin Wiley 6