What is Linked List in Java :
Linked List in java is a class that implements “List” interface. Linked List is a data structure that is used to hold or store objects in a structured manner. Linked List has a node that holds object and the address of next node. The first node is called “head” and last node is called as “tail”. The “head” holds the address of first node in the linked list java and the last node has “null” address value means last node does not have any address of next node (last node is the last item of this linked list).
In the above image, you can see each node has two parts : value of object and address of the next node.
The Head node holds the address value of first node and first node stores 21 as the value of an object of type Integer and 3500 as the address of next node.
The second node stores the 38 as the value of an object of type Integer and 3900 as the address of next node.
The third and last (Tail) node stores 10 as the value of an object of type Integer and null as the address of next node (Means there is no next node after the last node).
Why Linked List in Java ?
As we know that the Array stores the same data type of values and the size of the Array is pre- defined and later if you want to add any value of same data type in an Array at the run time then it was not possible in an Array.
Here to overcome this issue we use Linked List. Linked list allows dynamic memory allocation at run time means you can add any number of values in linked list during the execution of java program. See more below in advantages of linked list in java
Type of Linked List in Java :
- Singly Linked List
- Doubly Linked List
- Circular Linked List
Linked List Operations :
Advantages of Linked List in Java :
- Linked List is easy to use.
- Linked List can grow and shrink during the run time.
- Linked List.is easy to insert and delete objects
- Efficiently utilize the memory space.
- Linked List provides problem solving approach
- Linked List allow dynamic memory allocation.
Disadvantages of Linked List in Java :
- Linked List needs more memory space for storing next node address as compared to Array.
- Linked List is nor ordered by Index Position
- Accessing object is more difficult as compared to Array.
- To get the required object, Linked List has to traverse all the preceding objects.