In this tutorial, we will learn about the basic implementation of the Linked List data structure in Java with the help of coding examples.

First, we will see some theoretical information about the linked list data structure.

Then we will move to the implementation part and write a program to create and display the linked list in Java.

Let’s start the tutorial and begin with the basic introduction of a linked list in Java.

## Basic Introduction to Linked List and Its implementation

A linked list is also a linear data structure like Array. A linked list is the combination of nodes. We simply called each Node as an element of a linked list.

Each node has two basic components: data and a pointer ( or reference) which points to the next node of the linked list. If there is no next node present in the linked list, then it points to the null.

In the below section we will learn about the structure of the Linked list with the help of a diagram and then we will see its implementation in Java.

### Structure of Linked List

Below are some important points about the linked list in Java or in any other programming language and these points are very helpful when you solve the problem related to the linked list.

- The very first node of linked list is known as the head of the linked list.
- Last node of the linked list is called tail. As we discuss above last node of the linked list is always point to the null and it simply means there is no next node.
- If we talk about the traversal then each node in the linked list can be accessed by traversing the list from head to tail ( or start to end).

Consider the above-given diagram. In this diagram, a node with data as 9 is the head of the linked list, and a node with data 6 is the tail of the linked list. You can clearly see that the last node (i.e., tail) is pointing to the null.

You can clearly see that each node contains the reference ( reference in Java) or pointer( pointer in C or C++) to the next node.

This is the basic theoretical information about the Linked list in Java and next, we will see how we can create the Linked list in Java with the help of an implementation example.

### Algorithm to Create and Display the Linked List Implementation Java Example

Below are step-by-step algorithmic steps to create and display the Linked list in Java.

- Create a class Node with the two attributes: data and next. Since we are creating the linked list with the integer value, so datatype of attribute data is integer. Next is the pointer to the next node so it is simply a type of Node.
- Now create one more class and define two attribute head and tail of type Node.
will add a new element or node in the Linked list. This method takes data as a parameter and adds a new node with that data in the linked list in Java.*addElement()*- First it will check whether the linked list is empty or not.
- If the linked list is empty, then it simply means head is null and after adding the new element head and tail points to the newly added node.
- If the list is not empty, then newly added node will be added at the end of the linked list and now tails point the newly added node. Simply we can say the new node becomes the new tail of the linked list.

will display all the element of the linked list in Java.*displayElements()*- If the head is null, then simply write return and stops the method execution.
- If the head is not null, then assign head to the currentNode.
- Now traverse the linked list till the time current points to the null.
- Simply prints the currentNode.data in each iteration and make sure after printing the value current points to the next node of the linked list.

This is all about the theoretical information about the implementation steps of creating and displaying the linked list. Let’s see a program to demonstrate the creation and deletion of linked lists in Java.

```
package com.linkedlistimplemenation;
public class SinglyLinkedList {
Node head; //represents the head of the linked list
Node tail;// represents the tail of the linked list
//Below class represents the node of the linked list
class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
public void addElement(int data) {
//below line creates the new node
Node newNode = new Node(data);
/*now check head for null
or linked list for empty*/
if (head == null) {
/*since linked list is empty head
and tail points to same node*/
head = newNode;
tail = newNode;
} else {
//new node will be added after tail
// so tail next points to newNode
tail.next = newNode;
//now newNode becomes the new tail
tail = newNode;
}
}
public void displayElement() {
// node currentNode points the head
Node currentNode = head;
if (head == null) {
System.out.println("Linked list is empty: There are no elements");
return;
}
System.out.println("Linked List elements are: ");
while (currentNode != null) {
System.out.print(currentNode.data + " ");
currentNode = currentNode.next;
}
}
public static void main(String[] args) {
SinglyLinkedList singlyLinkedList = new SinglyLinkedList();
singlyLinkedList.addElement(9);
singlyLinkedList.addElement(2);
singlyLinkedList.addElement(6);
singlyLinkedList.displayElement();
}
}
```

#### Output

```
Linked List elements are:
9 2 6
```

#### Conclusion

A linked list is considered the best option when the frequent operation is insertion or deletion. Since elements are not stored on the contiguous location and there is no shifting required whenever we insert the element or delete the element in the middle or somewhere between in the list.

That’s all for this tutorial. Hope you like the tutorial and are able to understand the basic implementation of the linked list in Java.

Please share this tutorial with others if you find this informative and comment on your thoughts in the comment section down below.

Happy Learning.