# Deletion from the End of a Circular Linked List in JAVA

## JAVA Program to Delete a Node from the End of Circular Linked List

A Circular Linked List is collection on data elements where each element points towards the next element and the last Element points towards the First Element Hence forming  a Loop. Here is a Solution for the Deletion from the end of  Circular Linked List in JAVA. Deleting a node at the end of the circular linked means replacing the address of the second last node with the address of first node and this can be done very easily because circular linked list is a very flexible where the data isn’t store at contagious locations. ## Steps for Deletion of a Node from the End of a Circular Linked List in JAVA

• A head is the reference of the first node of the circular linked list which can be considered as the starting of the
list and points towards the next element.
• The tail is the last element of the circular linked list and it point towards the
first element making the list appear circular.
• If the list is not empty which means if the list has elements the last node will be the tail.
• To delete an element in the end of circular linked list, the present tail should be removed.
• By changing the node referred by the second last node to the first element , it has become the new tail.
• In this way the element is deleted from the end of the circular linked list. ## Below is the Algorithm to be followed in the Code

• deleteLast()
• return
• ELSE IF head != tail
• WHILE current->next != tail
• current = current-> next
• tail = current;
• tail -> next = head
• ELSE
• head = tail = null

## JAVA Program for Deleting a node from the End of a Circular Linked List

```public class Main {
public static void main(String[] args) {
Main Obj = new Main();
System.out.println("List Before Deletion");
Obj.print();
Obj.deleteLast();
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("Empty List");
}
else {
do{
//Prints each node by incrementing pointer.
System.out.print(" "+ current.element);
current = current.next;
System.out.println();
}
}

Node newNode = new Node(element);
tail = newNode;
}
else {
tail.next = newNode;
tail = newNode;
}
}
public void deleteLast() {
return;
}
else {