
Linked List in Java is a Collection class and it implements the doubly linked list data structure. LinkedList class in Java extends an abstract class AbstractSequentialList and implements the List and Deque interfaces. It implements all optional list operations and permits all elements, including null
.
For those, who are not familiar with a linked list data structure, it is a linear data structure that does not store the elements in contiguous locations and every element is a separate object with a data part and address part. In Linked list elements uses pointers and addresses to link each other. Each element in a linked list is known as a node. The benefits of using a linked list over Array are its simplicity and ease of insertions and deletions. The disadvantage of using the linked list is that it cannot be accessed directly. Instead, we need to start from the head and follow through the link to reach a node we wish to access.
There is two kinds of linked list, singly and doubly linked list. A singly-linked list allows traversing in one direction, mostly forward direction, whereas a doubly linked list allows to traverse in both directions, forward, and backward.
1. Create and Use Linked List in Java
The following implementation demonstrates how to create and use a linked list in Java.
OUTPUT
2. Java LinkedList Operations
Below we will see how to perform some basics operations on LinkedList –
2.1 Adding Elements to LinkedList
To add an element to a LinkedList, we can use the add() method. This method is overloaded to perform multiple operations based on different parameters. These methods to add elements are as follows –
- add(Element E): This method inserts an element at the end of the LinkedList.
- addFirst(Element E): This method inserts the specified element at the beginning of this list.
- addLast(Element E): This method inserts the specified element at the end of this list.
- add(int index, Element E): This method inserts an element at a specific index in the LinkedList.
Following Java code shows how to use add elements to a Linked List –
OUTPUT
2.2 Replacing or Changing an Elements in LinkedList
After adding the elements to Linked List, if you wish to change the element, it can be done using the set(int index, Element E) method. Following example shows how to replace an element in a linked list –
OUTPUT
2.3 Removing Elements from Linked List
In order to remove an element from a LinkedList, we can use the remove() method. There are different methods available to remove an element from Linked List. These methods are as follows –
- remove(Element): This method removes an element from the LinkedList. If there are multiple such objects, then it removes the first occurrence of the element.
- removeFirst(): This method removes and returns the first element from this list.
- removeLast(): Thiis method removes and returns the last element from the list
- remove(int index): This method takes an integer value and simply removes the element present at that specific index in the LinkedList. After removing the element, all the elements move to the left to fill the space and the indices of the elements are updated.
Following Java code shows how to use remove elements from a Linked List –
OUTPUT
Hope above details and examples help you to clarify your Java Linked List concepts.
If you like this post, please share among your friends.