# Deletion at the nth node of a Circular Linked List in 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.

## 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.
```public void deleteNthNode(int n)
{
int len = calcLen();

// Can only insert after 1st position
// Can't insert if position to insert is greater than size of Linked List
if(n < 1 || n > len)
{
System.out.println("Can't delete\n");

}
else
{
if(n == 1)
{
// head has to be deleted
return;
}
// required to traverse
Node previous = null;

// traverse to the nth node
while(--n > 0) {
previous = temp;
temp = temp.next;
}
// assigned next node of the previous node to nth node's next
previous.next = temp.next;
System.out.println("Deleted: " + temp.element);
}
}
```

## Code for deletion at the Nth node of a circular Linked List in Java

Run
```import java.util.*;

public class Main {
public static void main(String[] args) {
Main Obj = new Main();
System.out.println("List Before Deletion");
Obj.print();
Obj.deleteNthNode(2);
System.out.println("List After Deletion");
Obj.print();
}
public class Node{
int element;
Node next;

public Node(int element) {
this.element = element;
}
}
public Node tail = null;

public void print() {
System.out.println("null");
}
else {
do{
System.out.print(" "+ temp.element);
temp = temp.next;
System.out.println();
}
}

Node newNode = new Node(element);
tail = newNode;
}
else {
tail.next = newNode;
tail = newNode;
}
}
// Function return number of nodes present in list
public int Length()
{
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;
}

public int calcLen(){
int len = 0;

while(temp!=tail){
temp = temp.next;
len++;
}
return len;
}

public void deleteNthNode(int n)
{
int len = calcLen();

// Can only insert after 1st position
// Can't insert if position to insert is greater than size of Linked List
if(n < 1 || n > len)
{
System.out.println("Can't delete\n");

}
else
{
if(n == 1)
{
// head has to be deleted
return;
}
// required to traverse
Node previous = null;

// traverse to the nth node
while(--n > 0) {
previous = temp;
temp = temp.next;
}
// assigned next node of the previous node to nth node's next
previous.next = temp.next;
System.out.println("Deleted: " + temp.element);
}
}
}```

### Output

```List Before Deletion
10 20 30 40
List After Deletion
10 20 30```
Run
```import java.lang.*;
public class Main {
public static void main(String[] args) {
Main Obj = new Main();
System.out.println("List Before Deletion");
Obj.print();
Obj.deletenth(2);
System.out.println("List After Deletion");
Obj.print();

}
public class Node{
int element;
Node next;
Node prev;

public Node(int element) {
this.element = element;
}
}
public Node tail = null;
public void print() {
System.out.println("null");
}
else {
do{
System.out.print(" "+ temp.element);
temp = temp.next;
System.out.println();
}
}

Node newNode = new Node(element);
tail = newNode;
}
else {
tail.next = newNode;
newNode.prev=tail;
tail = newNode;
}
}
{
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;
}

public void deletenth (int n)
{
{
return;
}
else
{
int pos = n;
for (int i = 1; i < pos; i++)
{
current = current.next;
}
{
}
else if (current == null)
{
current = current.prev;
}
else
{
current.prev.next = current.next;
current.next.prev = current.prev;
}
//Delete the middle node
current = null;
}
}

void printList ()
{
//Node current will point to head
{
System.out.println ("List is empty");
return;
}
while (curr != null)
{
//Prints each node by increasing order of the pointer
System.out.print (curr.element + " ");
curr = curr.next;
}
System.out.println ();
}

}```

### Output

```List Before Deletion
10 20 30 40
List After Deletion
10 20 30
```

### Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

## Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

## Checkout list of all the video courses in PrepInsta Prime Subscription

• Introduction to Circular Linked List
• Circular Linked List in –
C | C++ | Java
• Insertion in Circular Linked List –
C | C++ | Java
• Insertion at the beginning–
C | C++ | Java
• Insertion at the end –
C | C++ | Java
• Insertion at nth position –
C | C++ | Java
• Deletion in Circular Linked List –
C | C++ | Java
• Deletion from beginning in Circular Linked List –
C | C++ | Java
• Deletion from nth position in Circular Linked List –
• Deletion from end in Circular Linked List –
C | C++ | Java
• Insertion and Deletion in Circular Linked List – C | C++ | Java
• Split a Circular Linked List in two halves –
C | C++ | Java
• Count nodes in Circular Linked List –
C | C++ | Java
• Sorted Insert In Circular Linked List –
C | C++ | Java
• Insertion in the middle in Circular Linked List –
C | C++ | Java