- Node(content of the element)
- Reference Address(pointer to the next node in the linked list)
LinkedList Implementation in Java
The JavaLinkedList class provides a doubly linked list implementation.
Each element in a singly linked list is known as a node. It consists of 2 fields:
- Next - Stores an address of the next element in the list. It is
nullfor the last element. - Data - Stores the actual data.
How to create a linkedList
Here is how we can create linked lists in Java:LinkedList<Type> linkedList = new LinkedList<>();
Here, Type indicates the type of a linked list. For example,
// create String type linked list
LinkedList<String> linkedList = new LinkedList<>();
// create Integer type linked list
LinkedList<Interger> linkedList = new LinkedList<>();
LinkedList using Interfaces
Let's take an example.List<String> food = new LinkedList<>();
Here we have declared a linked list, food, using the List interface. The linked list can only access the methods of the List interface.
Methods of LinkedList
LinkedList provides various methods that allow us to perform different operations in linked lists.
Add Elements to LinkedList
1. Add Elements: using the add() method To add an element (node) to the end of the linked list, we use theadd() method. For example,
Code:
import java.util.LinkedList;
class Main {
public static void main(String[] args){
LinkedList<String> food= new LinkedList<>();
// Add elements to LinkedList
food.add("Apple");
food.add("Mango");
food.add("Banana");
System.out.println("Food List: " + food);
}
}
Output
Food List: [Apple, Mango, Banana]
Accessing LinkedList Elements
1. Using get() Method : To access an element from the linked list, we can use theget() method. For example,
Code:
import java.util.LinkedList;
class Main {
public static void main(String[] args) {
LinkedList<String> food= new LinkedList<>();
// Add elements in the linked list
food.add("Apple"); food.add("Mango"); food.add("Banana"); System.out.println("Food List: " + food); // Get the element from the linked list String foodName = food.get(2); System.out.print("Element at index 1: " + foodName ); } }
Output
Food List: [Apple, Mango, Banana] Element at index 2: Banana
Searching LinkedList Elements
1. Using the contains() Method: To check if a linked list contains a particular element or not, we use thecontains() method. For example,
Code:
import java.util.LinkedList;
class Main {
public static void main(String[] args) {
LinkedList<String> food= new LinkedList<>();
// Add elements in the linked list
food.add("Apple");
food.add("Mango");
food.add("Banana");
System.out.println("Food List: " + food);
// Checks if Mango is in the food list
if(food.contains("Mango")) {
System.out.println("Mango is in Food List.");
}
}
}
Output
Food List: [Apple, Mango, Banana] Mango is in Food List.
Changing LinkedList Elements
1. Using the set() Method: To change elements of a linked list, we can use theset() method. For example,
Code:
// Change elements at index 2 food.set(2, "Kiwi");
System.out.println("New Food List: " + Kiwi);
Output
Food List: [Apple, Mango, Banana] New Food List: [Apple, Mango, Kiwi]
Removing LinkedList Elements
1. Using remove() Method: To remove an element from the linked list, we can use theremove() method. For example,
Code:
// Remove elements from index 1
String foodName= food.remove(1);
System.out.println("Removed Element: " + foodName);
Output
Food List: [Apple, Mango, Kiwi] Removed Element: Mango New Food List: [Apple, Kiwi] Also Read : Linked List