00:00

Deletion in Circular Linked List

Last updated:

Deleting from a circular linked list means unlinking a node while keeping the ring closed. The programs on this page hold a reference to the last node; the first node is always last.next. Whenever the first or last node is removed, these two links must be kept consistent, and a ring with a single node becomes an empty list.

This tutorial covers three cases:

  1. Delete node at the beginning

  2. Delete the node that holds a given value

  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

The first node is last.next. To remove it, make the last node skip over it: last.next = head.next. If the ring has only one node (head == last), the list becomes empty. No walk is needed, so this is O(1).

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 == 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 with a given value from the circular linked list

This program removes the first node whose data equals a given key. It walks the ring with two references, prev and head, until it finds the key or has checked the last node, then links prev to the node after the match. If the removed node was the last node, prev becomes the new last. The search makes this O(n).

DeleteAtSpecPositionInCLL.java
// Java program to delete the node that holds a given value (key)
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

To remove the last node, find the node just before it (the one whose next is last), point it at the first node, and make it the new last. A one-node ring becomes empty. Finding the second-last node requires a walk around the ring, so this is O(n) for a singly circular 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