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.
public void deleteLast() { if(head == null) { return; } else { if(head != tail ) { Node current = head; while(current.next != tail) { current = current.next; } tail = current; tail.next = head; } else { head = tail = null; } } }
Below is the Algorithm to be followed in the Code
- deleteLast()
- IF head == null
- return
- ELSE IF head != tail
- Node current = head
- WHILE current->next != tail
- current = current-> next
- tail = current;
- tail -> next = head
- ELSE
- head = tail = null
Code for deletion from the end of a circular Linked List in Java
import java.util.*; public class Main { public static void main(String[] args) { Main Obj = new Main(); Obj.add(10); Obj.add(20); Obj.add(30); Obj.add(40); 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 head = null; public Node tail = null; public void print() { Node temp = head; if(head == null) { System.out.println("null"); } else { do{ System.out.print(" "+ temp.element); temp = temp.next; }while(temp != head); System.out.println(); } } public void add(int element){ Node newNode = new Node(element); if(head == null) { head = newNode; tail = newNode; newNode.next = head; } else { tail.next = newNode; tail = newNode; tail.next = head; } } public void deleteLast() { if(head == null) { return; } else { if(head != tail ) { Node current = head; while(current.next != tail) { current = current.next; } tail = current; tail.next = head; } else { head = tail = null; } } } }
Output
List Before Deletion 10 20 30 40 List After Deletion 10 20 30
import java.util.*; public class Main { public static void main(String[] args) { Main Obj = new Main(); Obj.add(10); Obj.add(20); Obj.add(30); Obj.add(40); 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; Node prev; public Node(int element) { this.element = element; } } public Node head = null; public Node tail = null; public void print() { Node temp = head; if(head == null) { System.out.println("null"); } else { do{ System.out.print(" "+ temp.element); temp = temp.next; }while(temp != head); System.out.println(); } } public void add(int element){ Node newNode = new Node(element); if(head == null) { head = newNode; tail = newNode; newNode.next = head; newNode.prev=head; } else { tail.next = newNode; newNode.prev=tail; tail = newNode; tail.next = head; } } public void deleteLast() { if(head == null) { return; } else { if(head != tail ) { Node current = head; while(current.next != tail) { current = current.next; } tail = current; tail.next = head; head.prev=tail; } else { head = tail = null; } } } }
Output
List Before Deletion 10 20 30 40 List After Deletion 10 20 30
Prime Course Trailer
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
Circular Linked List
- Introduction to Circular Linked List
Click Here - Circular Linked List Applications
Click Here - Circular Linked List in –
- Insertion in Circular Linked List –
- Insertion at the beginning–
- Insertion at the end –
- Insertion at nth position –
- Deletion in Circular Linked List –
- Deletion from beginning in Circular Linked List –
- Deletion from nth position in Circular Linked List –
- Deletion from end in Circular Linked List –
- Insertion and Deletion in Circular Linked List – C | C++ | Java
- Split a Circular Linked List in two halves –
- Count nodes in Circular Linked List –
- Sorted Insert In Circular Linked List –
- Insertion in the middle in Circular Linked List –
Circular Linked List
- Introduction to Circular Linked List
- Circular Linked List Applications
- Circular Linked List in – C | C++ | Java
- Insertion in Circular Linked List – C | C++ | Java
- Deletion in Circular Linked List – C | C++ | Java
- Insertion and Deletion in a 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
Login/Signup to comment