LinkedList in Java: Methods
- The LinkedList class in Java provides us with the doubly linked list data structure.
- Each element of the linked list is known as a node.
- Each node points to the address of the next node & its previous node.
- Linked lists are preferred over the Array list because the insertion & deletion in the linked lists can be done in a constant time. But, in arrays, if we want to add or delete an element in between then, we need to shift all the other elements.
- In a linked list, it is impossible to directly access an element because we need to traverse the whole linked list to get the desired element.
ArrayList Vs. LinkedList :
Although ArrayList & LinkedList both implement the List interface and have the same methods, it is important to understand when to use which one.
- The insertion & deletion can be done in constant time in Linked List, so it is best to use the linked list when you need to add or remove elements frequently.
- Use ArrayList when you want to access the random elements frequently, as it can’t be done in a linked list in constant time.
Performing various operations on LinkedList :
-
Adding Element in LinkedList:
- Similar to ArrayList, add() method is used to add elements in a linked list.
- add(Object): Inserts an element at the end of the ArrayList.
- add(Index,Object) : Inserts an element at the given index.
Example :
Output :
Removing an element from the LinkedList:
- remove() method is used to remove an element from the linked list.
Example :
Output :
Changing An Element Of Linked List :
-
set() method is used to change an already existing element of a linked list.
Example :
Output :
Inserting an element at the last of the linked list:
-
addlast() method is used to insert an element at the start of the linked list.
Example :
Output :
Inserting an element at the start of the linked list:
-
addFirst() method is used to insert an element at the start of the linked list.
Example :
Output :
No comments:
Post a Comment