00:00

Deletion in Circular Linked List

In this Linked List tutorial, we will see all about the delete operation in a circular linked list. In the delete operation, we will delete an existing node from the circular linked list from any position.

In a Circular Linked List, we can delete a node at the following positions:

  1. Delete node at the beginning

  2. Delete node at the specific position

  3. Delete node at the end

Create a node class

Node.java
public class Node {
  public int data;
  public Node next;

  // Constructor
  public Node(int data) {
    this.data = data;
    this.next = null;
  }
}

1. Delete a node from the beginning of the circular linked list

In the below given example we will see how to delete an existing node from the beginning or front the circular linked list.

DeleteAtBeginningInCLL.java
// Java program to delete a node at the beginning
public class DeleteAtBeginningInCLL {
  // Method to delete first node
  static Node deleteFirstNode(Node last) {
    // Possibility 1: If list is an empty
    if (last == null) {
      System.out.println("List is empty, Deletion is not possible.");
      return null;
    }

    Node head = last.next;

    // Possibility 2: If list has only one element
    if (head.next == last) {
      last = null;
    } else {
      // Possibility 3: If list has more than one element
      last.next = head.next;
    }

    return last;
  }

  // Method to print list elements
  public static void printList(Node last) {
    if (last == null)
      return;

    Node head = last.next;
    while (true) {
      System.out.print(head.data + " ");
      head = head.next;
      if (head == last.next)
        break;
    }
    System.out.println();
  }

  public static void main(String[] args) {
    // Create a circular linked list
    // List: 50 -> 20 -> 40 -> 10 -> 30
    Node firstNode = new Node(50);
    firstNode.next = new Node(20);
    firstNode.next.next = new Node(40);
    firstNode.next.next.next = new Node(10);
    firstNode.next.next.next.next = new Node(30);

    Node lastNode = firstNode.next.next.next.next;
    lastNode.next = firstNode;

    System.out.print("Original List Elements: ");
    printList(lastNode);

    // Delete the first node
    lastNode = deleteFirstNode(lastNode);

    System.out.print("List elements, after deleting the first node: ");
    printList(lastNode);
  }
}
Output
Original List Elements: 50 20 40 10 30
List elements, after deleting the first node: 20 40 10 30

2. Delete a node at the specific position of the circular linked list

In the below given example we will see how to delete an existing node from the any position of the circular linked list.

DeleteAtSpecPositionInCLL.java
// Java program to delete a node at the specific position
public class DeleteAtSpecPositionInCLL {
  static Node deleteSpecificNode(Node last, int key) {
    // Possibility 1: If list is an empty
    if (last == null) {
      System.out.println("List is empty, Deletion is not possible.");
      return null;
    }

    Node head = last.next;
    Node prev = last;

    // Possibility 2: If list has only one element
    if (head == last && head.data == key) {
      last = null;
      return last;
    }

    // Possibility 3: If we want to delete first node from the list
    if (head.data == key) {
      last.next = head.next;
      return last;
    }

    // Possibility 4: If list has more elements
    // Traverse the list to find the node to be deleted
    while (head != last && head.data != key) {
      prev = head;
      head = head.next;
    }

    // If the node to be deleted is found
    if (head.data == key) {
      prev.next = head.next;
      if (head == last) {
        last = prev;
      }
    } else {
      // If the node to be deleted is not found
      System.out.println("Node with data " + key + " not found.");
    }
    return last;
  }

  // Method to print list elements
  public static void printList(Node last) {
    if (last == null) {
      System.out.println("List is Empty");
      return;
    }

    Node head = last.next;
    while (true) {
      System.out.print(head.data + " ");
      head = head.next;
      if (head == last.next)
        break;
    }
    System.out.println();
  }

  public static void main(String[] args) {
    // Create a circular linked list
    // List: 50 -> 20 -> 40 -> 10 -> 30
    Node firstNode = new Node(50);
    firstNode.next = new Node(20);
    firstNode.next.next = new Node(40);
    firstNode.next.next.next = new Node(10);
    firstNode.next.next.next.next = new Node(30);

    Node lastNode = firstNode.next.next.next.next;
    lastNode.next = firstNode;

    System.out.print("Original List Elements: ");
    printList(lastNode);

    // Delete a specific node (40) from the list
    int key = 40;
    lastNode = deleteSpecificNode(lastNode, key);

    System.out.print("List elements, after deleting the node " + key + ": ");
    printList(lastNode);
  }
}
Output
Original List Elements: 50 20 40 10 30
List elements, after deleting the node 40: 50 20 10 30

3. Delete a node from the end of the circular linked list

In the below given example we will see how to delete an existing node from the end or last the circular linked list.

DeleteAtEndInCLL.java
// Java program to delete the end or last node of circular linked list
public class DeleteAtEndInCLL {
  public static Node deleteLastNode(Node last) {
    // Possibility 1: If list is an empty
    if (last == null) {
      System.out.println("List is empty, Deletion is not possible.");
      return null;
    }

    Node head = last.next;

    // Possibility 2: If list has only one element
    if (head == last) {
      last = null;
      return last;
    }

    // Possibility 3: If list has more than one element
    Node temp = head;
    while (temp.next != last) {
      temp = temp.next;
    }

    temp.next = head;
    last = temp;

    return last;
  }

  // Method to print list elements
  public static void printList(Node last) {
    if (last == null)
      return;

    Node head = last.next;
    while (true) {
      System.out.print(head.data + " ");
      head = head.next;
      if (head == last.next)
        break;
    }
    System.out.println();
  }

  public static void main(String[] args) {
    // Create a circular linked list
    // List: 50 -> 20 -> 40 -> 10 -> 30
    Node firstNode = new Node(50);
    firstNode.next = new Node(20);
    firstNode.next.next = new Node(40);
    firstNode.next.next.next = new Node(10);
    firstNode.next.next.next.next = new Node(30);

    Node lastNode = firstNode.next.next.next.next;
    lastNode.next = firstNode;

    System.out.print("Original List Elements: ");
    printList(lastNode);

    // Delete the last node
    lastNode = deleteLastNode(lastNode);

    System.out.print("List elements, after deleting the last node: ");
    printList(lastNode);
  }
}
Output
Original List Elements: 50 20 40 10 30
List elements, after deleting the last node: 50 20 40 10