Updated March 28, 2023
Introduction to LinkedList in Java
LinkedList in Java is linear data structures that are different from arrays. There are certain advantages as well as disadvantages of using Linked List in a Java program. Each element in a linkedlist is stored in a cell known as Node. Each node has a specific address. The main disadvantage of LinkedList is that the nodes are not easily accessible at each point.
There are pointers that are used to address each node, and to access a specific node, there has to start from the head, and then the specific pointer to which the node is to be accessed is reached. The LinkedList class also consists of many constructors and methods like other Java interfaces. In this article, we are going to see two constructors which are being used in LinkedList.
They are:
- LinkedList(): This is used for assigning an empty LinkedList().
- LinkedList(Collection C): This is used to create an ordered list containing all elements of a specified collection, as returned by the collection’s iterator.
Methods of LinkedList in Java
There are many methods or functions which are part of the Java LinkedList class. We will see some of the functions that are part of the Java LinkedList class in this article.
They are:
- add(int a, I Element): This method is used for inserting a specific element at the specific position in this list.
- add( E e): This method fixes the specified element to the end of the list.
- add(int index, Collection C): This method inserts all the specified elements in the list, starting from the starting position.
- offerFirst(): This method Inserts the specified element at the front of this list.
- addLast(): This method is used for inserting an element at the end of the list.
- void clear(): This method is used for removing all the elements from the Linkedlist.
- poll(): It removes the first element of a list.
- lastIndexOf(): Used for returning the index of the last occurrence of the specified element in this list.
- getLast(): This function is used for returning the last element in the LinkedList.
- offer(): This method inserts the specified element as the tail element of the list.
- offerLast(): This method Inserts the specified element at the end of this list.
- peek(): It retrieves the first element of a list.
- peekFirst(): This method is used for retrieving the last element of a list or return null if the list is empty.
- addFirst(): This method is used for inserting the element at the beginning of the list.
- peekLast(): This method is used for retrieving the last element of the list or return null if the list is empty.
- pollFirst(): This method is used for retrieving and removing the first element of this list or returns null if this list is empty.
- contains(): This function returns true if the LinkedList contains the specific element at the node.
- pollLast(): This method removes the last element of this list or returns null if this list is empty.
- removeFirst(): This method returns the first element from this list.
- element(): This method retrieves but does not remove the head of the list.
- getFirst(): This method is used for returning the first element of the LinkedList.
- remove(): This method removes the first element of the LinkedList.
- remove(int index): This method removes the element at the specified position in this list.
- removeLast(): This method returns the last element from this list.
- set(int index, E element): This method replaces the element at the specified position in this list with the specified element.
- size(): This method returns the number of elements in this list.
Examples of LinkedList in Java
Given below are the examples mentioned:
Example #1
In this coding example, we are going to see the LinkedList methods of inserting certain elements in the linkedlist and then removing them, and finally displaying the linkedlist.
Code:
import java.util.*;
public class Example3
{
public static void main(String args[])
{
LinkedList<String> object = new LinkedList<String>();
// Adding elements to the linked list
object.add("A");
object.add("B");
object.addLast("C");
object.addFirst("D");
object.add(2, "E");
object.add("F");
object.add("G");
System.out.println("Linked list : " + object);
object.remove("C");
object.remove(3);
object.removeFirst();
object.removeLast();
System.out.println("Linked list after deletion: " + object);
}
}
Output:
In the sample output, we see that there are certain elements in the linkedlist, and finally, certain elements are deleted, and then the linkedlist after all the deletion of the elements is shown.
Example #2
In this program, we are going to see four names being printed using sequential order in LinkedList. We use a String LinkedList and use it to print names that can be of any number. We use the While loop here for printing the names which are present in the program.
Code:
import java.util.*;
public class LinkedList1
{
public static void main(String args[])
{
LinkedList<String> al=new LinkedList<String>();
al.add("Ishankamal Mitra");
al.add("Sourya Mukherjee");
al.add("Satyaki Das");
al.add("Debodooty Sarkar");
Iterator<String> itr=al.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
}
Output:
In this program, we check how the coding helps us to print four names in sequential order as mentioned in the LinkedList. In the next program, we are going to see how the sequence is changed; that is, the names are printed in reverse order of the input.
Example #3
In this code, the program inputs the name and then prints the names in the reverse order of their sequence.
Code:
import java.util.*;
public class LinkedList4
{
public static void main(String args[])
{
LinkedList<String> ll=new LinkedList<String>();
ll.add("Ishankamal Mitra");
ll.add("Sourya Mukherjee");
ll.add("Satyaki Das");
//Going through the list of elements in Reverse order
Iterator i=ll.descendingIterator();
while(i.hasNext())
{
System.out.println(i.next());
}
}
}
Output:
In this program, we use the DescendingIterator(), and we use it to print the names in the reverse order of the input. We can see it very clearly through the program.
Conclusion
In this article, we saw the different constructors and methods which are present in the LinkedList class. Plus, we saw a Java program to illustrate the insertion and deletion of elements in a LinkedList. We also saw the advantages and disadvantages of using LinkedList over arrays. They contain nodes that are not easily accessible and have to be accessed through the LinkedList head. We also notice three examples of coding where names are printed in reverse order, sequential order, and removing elements from a LinkedList. These programs help us to understand the methodology of the LinkedList class.
Recommended Articles
This is a guide to LinkedList in Java. Here we discuss the introduction and methods of linkedlist along with different examples and its code implementation. You may also look at the following articles to learn more –