  Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email  Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

# Implementing a Linked List in Java using Class

Last Updated on December 14, 2022 by Prepbytes

In this blog, we will discuss how to create linked list in java using class. We will deep dive to understand how the process of creating linked list is done in Java using classes and functions. Lets discuss our topic “create linked list in java using class”.

A Linked List is a linear data structure. Unlike arrays, the elements are not stored at contiguous locations. The linked list nodes are linked using pointers.

Structure of a node:
Each linked list node consists of 2 parts:

• Data: The Data which is stored at a particular address.
• Reference: Contains the address of the next node of the linked list.

Creating class Node:

```class Node {
int data;
Node next;

public Node(int data) {
this.data = data;
this.next = null;
}
}
```  • The first node of the list is called a head, and the last node of the list is called a tail.
• Each and every node in the list can be accessed by traversing through the list from head to tail.

In this article, we will be explaining how to add a new node in the linked list at the end or at any specified position. We will also explain how to delete any existing node in the Linked List.

## Insertion Operations

### Inserting at the end of the linked list

In this, we will insert a new node at the end of the Linked List.

For example, if we have a Linked List 2→4→6→8→10, and we want to insert 12 at the end of the linked list.

• After inserting 12 at the end of the given linked list, our updated linked list will be: 2→4→6→8→10→12.

### Algorithm To Create Linked List In Java Using Class

• As a Linked List is represented by the head pointer, so to insert a new node at the end of the linked list, we will use another pointer tail which will always point to the last node of the list.
• First, we will create a new node to be inserted, let’s say new_node, with given data.
• Then we will check if the head is null or not.
• If the head is null, then make:
• tail = new_node
• Else, if the head is not null, then make:
• tail.next = new_node
• tail = new_node

### Code Implementation To Create Linked List In Java Using Class

```public class InsertNode {
class Node {
int data;
Node next;

public Node(int data) {
this.data = data;
this.next = null;
}
}

public Node tail = null;

// Creating a new node
System.out.println("Adding a new node with value "+data+" at the end of the linked list ");
Node new_Node = new Node(data);

// it will check if the list is empty or not
// when list is empty,head and tail point to new node
tail = new_Node;
} else {
// new_Node will be added after tail such that tail's next will point to newNode
tail.next = new_Node;
// new_Node will become new tail of the list
tail = new_Node;
}
}

// PrintData() will display all the nodes present in the list
public void PrintData() {

return;
}
while (current != null) {
// It will print each node by incrementing pointer
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}

public static void main(String[] args) {

InsertNode List = new InsertNode();

// Add 5 to the end of the list
List.PrintData();

// Add 4 to the end of the list
List.PrintData();

// Add 3 to the end of the list
List.PrintData();

// Add 2 to the end of the list
List.PrintData();
}
}
```

Output
Adding a new node with value 5 at the end of the linked list
5
Adding a new node with value 4 at the end of the linked list
5 4
Adding a new node with value 3 at the end of the linked list
5 4 3
Adding a new node with value 2 at the end of the linked list
5 4 3 2

### Inserting at specific position in the linked list

In this method, we will traverse up to n elements, where n is equal to given position and add the node at that position.

For example, if we have a Linked List 1→2→3→4, and we want to insert a node with data = 5 at the 2nd position in the linked list.

• After inserting a new node with data = 5 at 2nd position in our linked list, our resultant linked list will look like: 1→2→5→3→4.
• Note: Take 0 based indexing while finding positions.

### Algorithm To Create Linked List In Java Using Class

• If the position is equal to 1, add the new_node before the head node.
• Otherwise, iterate up to the specified position using headNode and then do the following steps:

Note: Variables names in the algorithm are the same as that in code, so for better understanding, go through code and algorithm at the same time.

### Code Implementation To Create Linked List In Java Using Class

```public class InsertNode {
static class Node {
public int data;
public Node nextNode;

// Constructor
public Node(int data) {
this.data = data;

}
}

// Method to insert a node at position pos.
static Node Insert(Node headNode, int pos, int data) {
if (pos < 1)
System.out.print("Invalid pos");

if (pos == 1) {
Node new_node = new Node(data);
} else {
while (pos-- != 0) {
if (pos == 1) {

Node new_node = new Node(data);

break;
}
}
if (pos != 1)
System.out.print("Position out of bound");
}
}
// PrintData() will display all the nodes present in the list
static void PrintData(Node node) {
while (node != null) {
System.out.print(node.data);
node = node.nextNode;
if (node != null)
System.out.print("->");
}
System.out.println();
}

public static void main(String[] args) {

// Insert a node at any position
int data = 24, pos = 5;
System.out.print("Linked list after adding a node with data "+data+" at position "+pos+": ");

// Inserting a node at first position
data = 39;
pos = 1;
System.out.print("Linked list after adding a node with data "+data+" at position "+pos+": ");

data = 18;
pos = 7;
System.out.print("Linked list after adding a node with data "+data+" at position "+pos+": ");
}
}
```

Output
Linked list after adding a node with data 24 at position 5: 1->2->3->4->24
Linked list after adding a node with data 39 at position 1: 39->1->2->3->4->24
Linked list after adding a node with data 18 at position 7: 39->1->2->3->4->24->18

## Deletion Operations

In this section, we will see how to delete the first occurrence of the specified node from the linked list, i.e., deleting the first occurrence of a given key from the linked list.

For example, if we have a Linked List 2→4→6→4→10, and we want to delete the first occurrence of node with value 4 (key = 4) from the linked list.

• After deleting the first occurrence of node with value 4 from the linked list, our resultant linked list will be: 2→6→4→10.

### Algorithm To Create Linked List In Java Using Class

• Condition1: If the key is at the head.

• Change the head node to the next node of the current head.
• Condition 2: The key is either in the middle or last, except at the head.

• In this condition, find the previous node of the node to be deleted.

• prev = current_Node;

• current_Node = current_Node.next;

• Make the previous node of the node to be deleted point to the next node of the current node.

• prev.next = current_Node.next;

• Condition 3: The key is not in the list.

Note: Variables names in the algorithm are the same as that in code, so for better understanding, go through code and algorithm at the same time.

### Code Implementation To Create Linked List In Java Using Class

```public class LinkedList {

static class Node {

int data;
Node next;

// Constructor
Node(int d)
{
data = d;
next = null;
}
}

{

Node new_node = new Node(data);
new_node.next = null;

}
else {
while (last.next != null) {
last = last.next;
}

last.next = new_node;
}

return list;
}

{

while (current_Node != null) {
System.out.print(current_Node.data + " ");
current_Node = current_Node.next;
}
System.out.println();
}

// Method to delete the node with data equal to specified key
{

Node current_Node = list.head, prev = null;

// Condition 1: Key is  at head
if (current_Node != null && current_Node.data == key) {
System.out.println(key + " is found and deleted");

return list;
}

// Condition 2
while (current_Node != null && current_Node.data != key) {
prev = current_Node;
current_Node = current_Node.next;
}

if (current_Node != null) {
prev.next = current_Node.next;
System.out.println(key + " is found and deleted");
}

// Condition 3: The key is not  in the list
if (current_Node == null) {
}

return list;
}

// Driver code
public static void main(String[] args)
{

list = value(list, 2);
list = value(list, 6);
list = value(list, 1);
list = value(list, 8);
list = value(list, 9);
list = value(list, 0);
list = value(list, 3);
list = value(list, 5);

printData(list);
deleteNode(list, 2);
printData(list);

deleteNode(list, 8);
printData(list);

deleteNode(list, 20);
printData(list);
}
}
```

Output
Linked List :- 2 6 1 8 9 0 3 5
2 is found and deleted
Linked List :- 6 1 8 9 0 3 5
8 is found and deleted
Linked List :- 6 1 9 0 3 5
Linked List :- 6 1 9 0 3 5

This blog has discussed how to create linked list in java using class. Linked list is one of the most amazing topic of data structure and having knowledge of linked list is an plus point, you can follow this link Linked List for practicing more questions on linked list.

## FAQ

**1. Can we create linked list in Java?**
Like arrays, Linked List is a linear data structure. Unlike arrays, linked list elements are not stored at the contiguous location, the elements are linked using pointers. In Java, LinkedList can be represented as a class and a Node as a separate class.

**2. Why do we create linked list?**
Linked list is the data structure which can overcome all the limitations of an array. Using linked lists is useful because, It allocates the memory dynamically. All the nodes of the linked list are non-contiguously stored in the memory and linked together with the help of pointers.