Java LinkedList Tutorial
LinkedList is a kind of List used to store elements. Its an ordered collection i.e. elements added at a position in LinkedList will remain in that position till modified by user.
It internally uses a doubly linked list datastructure implementation i.e. each element is stored at separate node. Each node has a reference to previous and next node i.e
Advantage of LinkedList
Insertion and Deletion from the middle of LinkedList is very fast. Its just the matter of resetting 2 references.
Disadvantage of LinkedList:
LinkedList don’t provide effective random access of elements. It means if we want to access 5th element from LinkedList then we need to iterate through 1 to 5 elements to reach the 5th one. So, random access operations are very slow in LinkedList
Now, lets see how to create a LinkedList object in Java,
List<String> listOfStr = new LinkedList<>();
In Java, LinkedList implements List Interface, therefore we can keep the LinkedList object in List reference.
Now Lets see how we can use LinkedList through various examples,
- How to add elements in a LinkedList
- How to add elements at a particular index in LinkedList
- How to iterate over a LinkedList in Java
- How to iterate LinkedList in reverse or backward direction
If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.