The LinkedList is a linear data structure in Java that stores each element as an object. These objects are basically stored in non-contagious memory locations. In Java, the LinkedList can be implemented using the framework present in the “java.util” package, or custom classes can be designed to declare a LinkedList. There are various methods and functions that are implemented on the LinkedList to perform different operations like addition, removal, accessing, and iterating over the elements.
This article will discuss the implementation and working of a LinkedList in Java.
How Linked List is Created in Java?
The LinkedList is dynamic in nature which means that the size of the list grows dynamically as the elements are inserted so we don’t need to declare the size of the list. The elements in the LinkedList are stored separately at different memory locations. These elements are defined as “nodes” and these nodes are linked with each other using the address and the pointers.
The syntax of the LinkedList is depicted below:
LinkedList<Type> linkedList = new LinkedList<>();
Let us move to the next section of the article which is to create a LinkedList and perform certain operations on it.
Example 1: Creating a LinkedList in Java
The code below depicts the creation of a LinkedList in Java.
import java.util.*;
//Create a class for declaring a linked list
public class Linkedlist {
public static void main(String args[])
{
//Creating LinkedList object
LinkedList<String> s = new LinkedList<String>();
// The add() method adds the elements to the LinkedList
s.add("Programming");
s.add("in");
//This method adds the element in the end
s.addLast("Java");
//The method adds the element to the start
s.addFirst("Welcome to");
System.out.println(s);
}
}
In the above code:
- The required package is imported.
- The next step involves the creation of a LinkedList.
- The add() method adds the elements to the LinkedList.
- An element “Java” is added to the list using the addLast() method.
- To add the element to the start of the element addFirst() method is implemnetd.
- The println() method prints the elements and the operations on it.
Output
The output below depicts that the elements are printed in an order.

Operations on a LinkedList in Java
The following operations are performed on the LinkedList in Java. The operations are listed below.
- Adding the elements
- Removing the elements
- Changing the elements
- Iterating over the elements
And the size() method to calculate the size of the LinkedList.
- Adding the Elements
The elements to the LinkedList are added using the add() method that is described below.
import java.util.*;
//Create a class for declaring a linked list
public class Linkedlist {
public static void main(String args[])
{
//Creating LinkedList object
LinkedList<String> s = new LinkedList<String>();
// The add() method adds the elements to the LinkedList
s.add("Programming");
s.add("in");
s.add("Java");
s.add("is");
s.add("fun");
//This method adds the element in the end
System.out.println(s);
}
}
In the above block of code:
- The required package is imported and the class is declared for the LinekdList.
- The object “s” is created for the Linekdlist.
- The add() method is used to add the elements to the linked list using the object created.
- The println() method prints the output accordingly.
Output
The below output shows that the elements are printed in square brackets as:

- Removing the Element of the LinkedList
The below code implements the remove() method in two different ways. The first method declares the string itself that is to be removed and the second method uses the index to remove the specified string.
// Import the required package
import java.util.*;
//Declare a class for Linked List
public class RemoveElement {
//The Main class of Java
public static void main(String args[])
{
LinkedList<String> r = new LinkedList<>();
//The add() method adds the elements to the LinkedList
r.add("Python");
r.add("Java");
r.add(2, "C++");
r.add(3,"Javascript");
System.out.println("Original LinkedList " + r);
//remove() method to remove the specified element
r.remove(2);
System.out.println("LinkedList when index is removed " + r);
r.remove("Java");
System.out.println("LinkedList when the string is removed"+ r);
}
}
In the above code:
- The required package is declared.
- In the next step, an object for a LinkedList is created as “r”.
- The add() method in Java adds the elements to the LinkedList.
- The println() method prints the original list without any modifications.
- The remove() method removes the element with index number 2 and String “Java”
- The LinkedList after removal is printed using the println() method.
Output
The below output depicts the elements with the original LinkedList and the elements after the removal are printed.

- Changing the Elements
The code below describes the set() method implementation to change the elements in the LinkedList.
// Import the required package
import java.util.*;
//Declare a class for Linked List
public class ChangeElement {
//The Main class of Java
public static void main(String args[])
{
LinkedList<String> r = new LinkedList<>();
//The add() method adds the elements to the LinkedList
r.add("Python");
r.add("Java");
r.add(2, "C++");
r.add(3,"Javascript");
System.out.println("Original LinkedList " + r);
r.set(3,"Ruby");
System.out.println("Modified LinkedList " + r);
}
}
In the above code, using the set() method the element at the specified index is changed. The original and modified lists are printed.
Output
The below output depicts that the “Javascript” at index position 3 is replaced with “Ruby” in the LinkedList.

- Iterating through the Elements in Java
The iteration in the LinkedList is done through different methods. The code below depicts the for loop that iterates over the elements of the LinkedList.
// Import the required package
import java.util.*;
//Declare a class for Linked List
public class IterateElement {
//The Main class of Java
public static void main(String args[])
{
LinkedList<String> r = new LinkedList<>();
//The add() method adds the elements to the LinkedList
r.add("Python");
r.add("Java");
r.add(2, "C++");
r.add(3, "Javascript");
// Using the for loop with the get() method
for (int x = 0; x < r.size(); x++) {
System.out.print(r.get(x) + " ");
}
}
}
In the above code, a for loop is declared with the get() method to print the elements present in the LinkedList.
Output
The below output is printed as the result of the get() method of LinkedList.

- Size/Length of the LinkedList
Size is an important aspect of a LinkedList therefore the size() method returns the number of elements present in the LinkedList. The below code implements the size() method.
// Import the required package
import java.util.*;
//Declare a class for Linked List
public class ListSize {
//The Main class of Java
public static void main(String args[])
{
LinkedList<String> r = new LinkedList<>();
//The add() method adds the elements to the LinkedList
r.add("Python");
r.add("Java");
r.add(2, "C++");
r.add(3,"Javascript");
// Using the for loop with the get() method
System.out.println("The size of the given linked list is: " + r.size());
}
}
In the code block, the size() method of Java returns the total number of elements currently present in the LinkedList.
Output
In the output below, the size is returned as “4” since there are four elements in the LinkedList.

This article has effectively demonstrated the topic of LinkedList in Java.
Conclusion
The method to create a LinkedList in Java involves declaring the type of the LinkedList and initializing an object for that LinkedList. Once a LinkedList is created, different built-in functions can be used to manipulate that LinkedList. In this blog post, we have demonstrated in detail how LinkedLists are created in Java and what are the basic operations performed on the LinkedList.