Last Updated: 02 October, 2023
A linked list is basically a data structure that consists of a sequence of nodes, where each node contains a data element and a reference to the next node in the sequence. The first node in the list is called the head node, and the last node is called the tail node.
To create a linked list in Java, we use the LinkedList class, which was introduced in the Java 1.2 version and is available in the java.util package.
LinkedList is a doubly-linked list implementation of the List and Deque interfaces and provides a number of methods for adding, removing, and traversing the list.
✅ Best Choice: If our frequent operation is insertion or deletion in the middle of a list.
❌Worst Choice: If our frequent operation is a retrieval operation, each node contains extra memory to store the previous and next node addresses.
As we can see from the complete LinkedList class hierarchy above, the LinkedList class extends the AbstractSequentialList class and implements List, Deque, Cloneable, and Serializable Interfaces.
Here, E defines the type of elements that the List will contain.
Output
LinkedList Elements: [India, Canada, US, England]
The Java LinkedList class has the following constructors with descriptions:
Constructor | Description |
---|---|
LinkedList() | Constructs an empty list. |
LinkedList(Collection<? extends E> c) | Constructs a list containing the elements of the specified collection in the order they are returned by the collection's iterator. |
The Java LinkedList class has the following methods with descriptions:
Method | Description |
---|---|
boolean add(E element) | Appends the given element to the list. |
boolean addAll(Collection c) | Appends the given collection of elements to the list. |
void add(int index, E element) | Inserts the given element at the specified index. |
boolean addAll(int index, Collection c) | Inserts the given collection of elements at the specified index. |
void addFirst(E element) | Inserts the given element at the beggining of the list. |
void addLast(E element) | Inserts the given element at the end of the list. |
boolean offer(E element) | Inserts the given element at the end of the list. |
boolean offerFirst(E element) | Inserts the given element at the beggining of the list. |
boolean offerLast(E element) | Inserts the given element at the end of the list. |
void push(E element) | Inserts the given element at the beggining of the list. |
E get(int index) | Returns an element at a specified index from the list. |
E getFirst() | Returns the first element from the list. |
E getLast() | Returns the last element from the list. |
E element() | Returns the first element from the list. |
E peek() | Returns the first element from the list. |
E peekFirst() | Returns the first element from the invoking LinkedList, and returns null if a list is empty. |
E peekLast() | Returns the last element from the invoking LinkedList, and returns null if a list is empty. |
int indexOf(E element) | Returns the index value of the first element to occur in the list. |
int lastIndexOf(E element) | Returns the index value of the given element's last appearance in the list. |
E set(int index, E newElement) | Replace the element at the specified index with a new element in the invoking list. |
E remove() | Removes the first element from the invoking list. |
E remove(int index) | Removes the element at the specified index in the invoking list. |
boolean remove(Object element) | Removes the first occurrence of the given element from the invoking list. |
boolean removeAll(Collection c) | Removes the given collection of elements from the invoking LinkedList. |
E removeFirst() | Removes the first element from the invoking list. |
E removeLast() | Removes the first element from the invoking list. |
E removeFirstOccurrence(Object element) | Removes the first occurrence of the given element from the invoking LinkedList. |
E removeLastOccurrence(Object element) | Removes the last occurrence of the given element from the invoking LinkedList. |
E poll() | Removes the first element from the LinkedList, and returns null if the list is empty. |
E pollFirst() | Removes the first element from the LinkedList, and returns null if the list is empty. |
E pollLast() | Removes the last element from the LinkedList, and returns null if the list is empty. |
E pop() | Removes the first element from the LinkedList. |
void clear() | Removes all the elements from the LinkedList. |
int size() | Returns the total number of elements in the invoking LinkedList. |
boolean isEmpty() | Returns true if the list is empty; otherwise, it returns false. |
boolean contains(Object element) | Returns true if the list contains the given element; otherwise, it returns false. |
void sort(Comparator c) | Sorts all the elements of the invoking list based on the given comparator. |
List[] subList(int startIndex, int endIndex) | Returns a list of elements starting from startIndex to endIndex-1. |
Object clone() | Returns a shallow copy of a LinkedList. |
Object[] toArray() | Returns an array of object instances that contains all the elements from invoking LinkedList. |
Spliterator spliterator() | Creates spliterator over the elements in a list. |
void trimToSize() | Used to trim a LinkedList instance to the number of elements it contains. |
The Java LinkedList class provided methods are implemented in the below given examples. Let's go through the examples one by one and understand the uses for each method.
Example 1 : Methods for Adding elements in LinkedList
Output
LinkedList Elements: [100, 200, 500]
After list.addFirst(50): [50, 100, 200, 500]
After list.addLast(550): [50, 100, 200, 500, 550]
After list.add(2, 300): [50, 100, 300, 200, 500, 550]
list.push(700): [700, 50, 100, 300, 200, 500, 550]
After list.addAll(childList): [700, 50, 100, 300, 200, 500, 550, 600, 700]
After list.addAll(3, childList): [700, 50, 100, 600, 700, 300, 200, 500, 550, 600, 700]
After adding array: [700, 50, 100, 600, 700, 300, 200, 500, 550, 600, 700, 1000, 2000, 3000]
Elements: [A, B, C, D, E]
Example 2 : Methods for Accessing elements from LinkedList
Output
LinkedList Elements: [A, B, C, D, E, F, G, H, I, J, K]
list.get(4): E
list.subList(2, 5): [C, D, E]
list.element(): A
list.getFirst(): A
list.getLast(): K
Traversing the list in the reverse order:
KJIHGFEDCBA
Traversing the list using for loop:
ABCDEFGHIJK
Example 3 : Methods for Updating elements in LinkedList
Output
LinkedList Elements: [A, B, C, D, E, F, G, H, I, J, K]
After list.set(3, 'X'): Z
LinkedList Elements: [A, B, C, X, E, F, G, H, I, J, K]
Example 4 : Methods for Removing elements from LinkedList
Output
LinkedList Elements: [A, B, C, D, E, F, G, H, I, J, K]
After list.remove(): [B, C, D, E, F, G, H, I, J, K]
list.pop(): B
After list.pop(): [C, D, E, F, G, H, I, J, K]
After list.remove(2): [C, D, F, G, H, I, J, K]
list.removeFirst(): C
list.removeFirst(): [D, F, G, H, I, J, K]
list.removeLast(): K
list.removeLast(): [D, F, G, H, I, J]
list.removeFirstOccurrence('H'): true
After list.removeFirstOccurrence('H'): [D, F, G, I, J]
list.removeLastOccurrence('G'): true
After list.removeLastOccurrence('G'): [D, F, I, J]
After list.removeAll(childList): [D, F, J]
Example 5 : Utility methods of LinkedList
Output
LinkedList Elements: [A, B, C, D, E, F, G, H, I, J, K]
list.isEmpty(): false
list.size(): 11
list.indexOf('D'): 3
list.contains('D') : true
list.containsAll(childList): false
After list.clear(): []
Reference: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/LinkedList.html
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com
What is the difference between an ArrayList and a LinkedList?
Ans. Both have the following differences:
ArrayList | LinkedList |
---|---|
ArrayList internally uses a dynamic array to store the elements. | LinkedList internally uses a doubly linked list to store the elements. |
ArrayList implements a List interface. Therefore, this acts as a list. | LinkedList implements both the List interface and the Deque interface. Therefore, it can act as a list and a deque. |
ArrayList is slow as array manipulation is slower. | LinkedList is faster than node-based as there is not much bit shifting required. |
ArrayList is best for storing and accessing elements from the list. | LinkedList is best for manipulating the elements. |
ArrayList allocates contiguous memory locations. | LinkedList does not allocate contiguous memory locations. |
The default capacity of an ArrayList is 10. | LinkedList has no default capacity. An empty list is created when a LinkedList is initialized. |