Prime #### Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime #### Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

# Deletion at the nth node of a Circular Linked List in Java ## JAVA Program to delete a Node at the nth node of a Circular Linked List

In the process of deletion at the nth node of a circular linked list we first check if there is some data present in the list or not as deletion is not possible form an empty list. If the list is not empty we traverse till the specific position and delete the specific node using the algorithm mentioned in this article below. We can also perform deletion from:-

• Beginning of the circular linked list.
• End of the circular linked list.

Read more about data structures and algorithm by clicking the button below.

## Given below is the description of deletion from the nth Node of a Circular Linked List

Deletion from nth position in a circular linked list –

• If the list is empty we cannot delete element from it hence return as deletion is not possible.
• If there is data present in the nodes of the list, then
• Traverse till the specific position from where you want to delete an element.
• Then make next pointer of previous list as next of next node.
• Free the node that is present at specific position. ## To delete an element from the nth Position of a Circular Linked List following Algorithm is to be followed

• EXIT
• ELSE IF (index>=len||index<0)
• EXIT
• WHILE(len>0)
• if(index==count)
• previous.next=next.next
• End if
• previous=previous.next
• next=previous.next
• len–
• count++

## Java program for deleting a node from the nth node of a Circular Linked List

```import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
// structure for a node
static class Node
{
int data;
Node next;
};
// Function to insert a node at the end of a Circular linked list
static Node Insert(Node head, int data)
{

// Create a new node
Node newNode = new Node();

// check node is created or not
if (newNode == null)
{
System.out.printf("\nMemory Error\n");
return null;
}

// insert data into newly created node
newNode.data = data;

// check list is empty
// if not have any node then
// make first node it
{
newNode.next = newNode;
}

// if list have already some node
else
{
// move first node to last node
{
current = current.next;
}
// put new node address into last
current.next = newNode;
}
}
// Function print data of list
{
// if list is empty, simply show message
{
System.out.printf("\nDisplay List is empty\n");
return;
}
// traverse first to last node
else
{
do
{
System.out.printf("%d ", current.data);
current = current.next;
}
}
// Function return number of nodes present in list
{
int count = 0;
// if list is empty
// simply return length zero
{
return 0;
}
// traverse forst to last node
else
{
do
{
current = current.next;
count++;
}
return count;
}
static Node DeleteAtPosition( Node head, int index)
{
// Find length of list
int count = 0;
// check list have any node
// if not then return
{
System.out.printf("\nDelete Last List is empty\n");
return null;
}
// given index is in list or not
if (index >= len || index < 0)
{
return null;
}
while (len > 0)
{
// if index found delete that node
if (index == count)
{
previous.next = next.next;

}
previous = previous.next;
next = previous.next;
len--;
count++;
}
}
// Driver Code
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
int n,data;
System.out.println("Enter the number of nodes you want to insert\n");
n=sc.nextInt();
System.out.println("Enter the data for the nodes\n");
for(int i=0;i<n;i++)
{
data=sc.nextInt();
}
// Deleting Node at position
System.out.printf("Initial List: ");
System.out.printf("\nEnter the index of node which you want to delete ");
int del;
del=sc.nextInt();
}
} ```
```Output:
Enter the number of nodes you want to insert

5
Enter the data for the nodes

11
12
13
14
15
Initial List: 11 12 13 14 15
Enter the index of node which you want to delete 3
11 12 13 14```