Deletion from the Beginning of a Circular Linked List in JAVA
JAVA Program to Delete a Node from the Beginning of Circular Linked List
Shifting of elements is not required for the deletion to happen in a circular linked list. To delete a node at the beginning you just need to delete the reference of head and give it a new reference instead. This might sound like a lengthy process but it really isn’t. Below in the page in which we’ll learn in-depth about deletion a Node from the Beginning of a Circular Linked List in JAVA.
Steps Delete a Node from the Beginning of a Circular Linked List
- In a circular linked list a reference variable head points to the first node. The first node stores the information of the next node of the circular linked.
- The tail is the last node that points back to the first node making the linked list circular because the traversal can be done from start till end.
- To delete an element in the beginning of the linked list. The first node should be deleted and the reference should point towards the second node making it the first element of the circular linked list.
- To delete a node in the beginning means to change the node tail is referring to second node. By doing this the second node will become the first node of the circular linked list and the node from the beginning will be deleted.
public void deleteFirst() { if(head == null) { return; } else { if(head != tail ) { head = head.next; tail.next = head; } else { head = tail = null; } } }
Algorithm for deletion from beginning of a circular linked list
- deleteFirst()
- IF head == null
- return
- ELSE IF head != tail
- head = head -> next
- tail -> next = head
- ELSE
- head = tail = null
Code for deletion from the beginning of a circular Linked List in Java
Method 1
Method 2
Method 1
Run
import java.lang.*; 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.deleteFirst(); 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 deleteFirst() { if(head == null) { return; } else { if(head != tail ) { head = head.next; tail.next = head; } else { head = tail = null; } } } }
Output
List Before Deletion 10 20 30 40 List After Deletion 20 30 40
Method 2
Run
import java.lang.*; 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.deleteFirst(); 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 deleteFirst() { if(head == null) { return; } else { if(head != tail ) { head = head.next; tail.next = head; } else { head = tail = null; } } } }
Output
List Before Deletion 10 20 30 40 List After Deletion 20 30 40
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