Open In App

Introduction to Circular Linked List

What is Circular linked list?

The circular linked list is a linked list where all nodes are connected to form a circle. In a circular linked list, the first node and the last node are connected to each other which forms a circle. There is no NULL at the end.

circular-linked-list-copy

Circular linked list

There are generally two types of circular linked lists:

Representation of Circular singly linked list

Representation of circular doubly linked list

Note: We will be using the singly circular linked list to represent the working of the circular linked list.

Representation of circular linked list:

Circular linked lists are similar to single Linked Lists with the exception of connecting the last node to the first node.

Node representation of a Circular Linked List:

// Class Node, similar to the linked list
class Node{
    int value;

// Points to the next node.
    Node next;
}
struct Node {
    int data;
    struct Node *next;
};
public class Node {
    int data;
    Node next;
    
    public Node(int data) {
        this.data = data;
        this.next = null;
    }
}
public class Node
{
    public int data;
    public Node next;
    
    public Node(int data)
    {
        this.data = data;
        this.next = null;
    }
}
class Node {
  constructor(data) {
    this.data = data;
    this.next = null;
  }
}
class Node {
  public $data;
  public $next;

  function __construct($data) {
    $this->data = $data;
    $this->next = null;
  }
}
# Class Node, similar to the linked list
class Node:
    def __init__(self,data):
        self.data = data
        self.next = None

Example of Circular singly linked list:

Example of  circular linked list

The above  Circular singly linked list can be represented as:

// Initialize the Nodes.
Node one = new Node(3);
Node two = new Node(5);
Node three = new Node(9);

// Connect nodes
one.next = two;
two.next = three;
three.next = one;
Node* one = createNode(3);
Node* two = createNode(5);
Node* three = createNode(9);

// Connect nodes
one->next = two;
two->next = three;
three->next = one;
// Define the Node class
class Node {
    int value;
    Node next;

    public Node(int value) {
        this.value = value;
    }
}

// Initialize the Nodes.
Node one = new Node(3);
Node two = new Node(5);
Node three = new Node(9);

// Connect nodes
one.next = two;
two.next = three;
three.next = one;
Node one = new Node(3);
Node two = new Node(5);
Node three = new Node(9);

// Connect nodes
one.next = two;
two.next = three;
three.next = one;
let one = new Node(3);
let two = new Node(5);
let three = new Node(9);

// Connect nodes
one.next = two;
two.next = three;
three.next = one;
$one = new Node(3);
$two = new Node(5);
$three = new Node(9);

// Connect nodes
$one->next = $two;
$two->next = $three;
$three->next = $one;
# Initialize the Nodes.
one = Node(3)
two = Node(5)
three = Node(9)

# Connect nodes
one.next = two
two.next = three
three.next = one

Explanation: In the above program one, two, and three are the node with values 3, 5, and 9 respectively which are connected in a circular manner as:

Operations on the circular linked list:

We can do some operations on the circular linked list similar to the singly linked list which are:

  1. Insertion
  2. Deletion

1. Insertion in the circular linked list:

A node can be added in three ways:

  1. Insertion at the beginning of the list
  2. Insertion at the end of the list
  3. Insertion in between the nodes

1) Insertion at the beginning of the list: To insert a node at the beginning of the list, follow these steps: 

Circular linked list before insertion

And then, 

Circular linked list after insertion

2) Insertion at the end of the list: To insert a node at the end of the list, follow these steps: 

Before insertion,

Circular linked list before insertion of node at the end

After insertion,

Circular linked list after insertion of node at the end

3) Insertion in between the nodes: To insert a node in between the two nodes, follow these steps: 

Suppose 12 needs to be inserted after the node has the value 10,

Circular linked list before insertion

After searching and insertion,

Circular linked list after  insertion

2. Deletion in a circular linked list:

1) Delete the node only if it is the only node in the circular linked list:

2) Deletion of the last node:

3) Delete any node from the circular linked list: We will be given a node and our task is to delete that node from the circular linked list.

Algorithm:
Case 1: List is empty. 

Case 2:List is not empty  

Below is the implementation for the above approach:

// C++ program to delete a given key from
// linked list.
#include <bits/stdc++.h>
using namespace std;

// Structure for a node
class Node {
public:
    int data;
    Node* next;
};

// Function to insert a node at the
// beginning of a Circular linked list
void push(Node** head_ref, int data)
{

    // Create a new node and make head
    // as next of it.
    Node* ptr1 = new Node();
    ptr1->data = data;
    ptr1->next = *head_ref;

    // If linked list is not NULL then
    // set the next of last node
    if (*head_ref != NULL) {

        // Find the node before head and
        // update next of it.
        Node* temp = *head_ref;
        while (temp->next != *head_ref)
            temp = temp->next;
        temp->next = ptr1;
    }
    else

        // For the first node
        ptr1->next = ptr1;

    *head_ref = ptr1;
}

// Function to print nodes in a given
// circular linked list
void printList(Node* head)
{
    Node* temp = head;
    if (head != NULL) {
        do {
            cout << temp->data << " ";
            temp = temp->next;
        } while (temp != head);
    }

    cout << endl;
}

// Function to delete a given node
// from the list
void deleteNode(Node** head, int key)
{

    // If linked list is empty
    if (*head == NULL)
        return;

    // If the list contains only a
    // single node
    if ((*head)->data == key && (*head)->next == *head) {
        free(*head);
        *head = NULL;
        return;
    }

    Node *last = *head, *d;

    // If head is to be deleted
    if ((*head)->data == key) {

        // Find the last node of the list
        while (last->next != *head)
            last = last->next;

        // Point last node to the next of
        // head i.e. the second node
        // of the list
        last->next = (*head)->next;
        free(*head);
        *head = last->next;
        return;
    }

    // Either the node to be deleted is
    // not found or the end of list
    // is not reached
    while (last->next != *head && last->next->data != key) {
        last = last->next;
    }

    // If node to be deleted was found
    if (last->next->data == key) {
        d = last->next;
        last->next = d->next;
        free(d);
    }
    else
        cout << "Given node is not found in the list!!!\n";
}

// Driver code
int main()
{
    // Initialize lists as empty
    Node* head = NULL;

    // Created linked list will be
    // 2->5->7->8->10
    push(&head, 2);
    push(&head, 5);
    push(&head, 7);
    push(&head, 8);
    push(&head, 10);

    cout << "List Before Deletion: ";
    printList(head);

    deleteNode(&head, 7);

    cout << "List After Deletion: ";
    printList(head);

    return 0;
}
#include <stdio.h>
#include <stdlib.h>

// Structure for a node
struct Node {
    int data;
    struct Node* next;
};

// Function to insert a node at the
// beginning of a Circular linked list
void push(struct Node** head_ref, int data)
{

    // Create a new node and make head
    // as next of it.
    struct Node* ptr1 = (struct Node*)malloc(sizeof(struct Node));
    ptr1->data = data;
    ptr1->next = *head_ref;

    // If linked list is not NULL then
    // set the next of last node
    if (*head_ref != NULL) {

        // Find the node before head and
        // update next of it.
        struct Node* temp = *head_ref;
        while (temp->next != *head_ref)
            temp = temp->next;
        temp->next = ptr1;
    }
    else

        // For the first node
        ptr1->next = ptr1;

    *head_ref = ptr1;
}

// Function to print nodes in a given
// circular linked list
void printList(struct Node* head)
{
    struct Node* temp = head;
    if (head != NULL) {
        do {
            printf("%d ", temp->data);
            temp = temp->next;
        } while (temp != head);
    }

    printf("\n");
}

// Function to delete a given node
// from the list
void deleteNode(struct Node** head, int key)
{

    // If linked list is empty
    if (*head == NULL)
        return;

    // If the list contains only a
    // single node
    if ((*head)->data == key && (*head)->next == *head) {
        free(*head);
        *head = NULL;
        return;
    }

    struct Node *last = *head, *d;

    // If head is to be deleted
    if ((*head)->data == key) {

        // Find the last node of the list
        while (last->next != *head)
            last = last->next;

        // Point last node to the next of
        // head i.e. the second node
        // of the list
        last->next = (*head)->next;
        free(*head);
        *head = last->next;
        return;
    }

    // Either the node to be deleted is
    // not found or the end of list
    // is not reached
    while (last->next != *head && last->next->data != key) {
        last = last->next;
    }

    // If node to be deleted was found
    if (last->next->data == key) {
        d = last->next;
        last->next = d->next;
        free(d);
    }
    else
        printf("Given node is not found in the list!!!\n");
}

// Driver code
int main()
{
    // Initialize lists as empty
    struct Node* head = NULL;

    // Created linked list will be
    // 2->5->7->8->10
    push(&head, 2);
    push(&head, 5);
    push(&head, 7);
    push(&head, 8);
    push(&head, 10);

    printf("List Before Deletion: ");
    printList(head);

    deleteNode(&head, 7);

    printf("List After Deletion: ");
    printList(head);

    return 0;
}
// Java program to delete a given key from
// linked list.
import java.io.*;
import java.util.*;

public class GFG {
    /* structure for a node */
    static class Node {
        int data;
        Node next;
    };

    /* Function to insert a node at the beginning of
a Circular linked list */
    static Node push(Node head_ref, int data)
    {
        // Create a new node and make head as next
        // of it.
        Node ptr1 = new Node();
        ptr1.data = data;
        ptr1.next = head_ref;

        /* If linked list is not null then set the
next of last node */
        if (head_ref != null) {
            // Find the node before head and update
            // next of it.
            Node temp = head_ref;
            while (temp.next != head_ref)
                temp = temp.next;
            temp.next = ptr1;
        }
        else
            ptr1.next = ptr1; /*For the first node */

        head_ref = ptr1;
        return head_ref;
    }

    /* Function to print nodes in a given
circular linked list */
    static void printList(Node head)
    {
        Node temp = head;
        if (head != null) {
            do {
                System.out.printf("%d ", temp.data);
                temp = temp.next;
            } while (temp != head);
        }

        System.out.printf("\n");
    }

    /* Function to delete a given node from the list */
    static Node deleteNode(Node head, int key)
    {
        if (head == null)
            return null;
        int flag = 0;
        // Find the required node
        Node curr = head, prev = new Node();
        while (curr.data != key) {
            if (curr.next == head) {
                System.out.printf(
                    "Given node is not found in the list!!!\n");
                flag = 1;
                break;
            }

            prev = curr;
            curr = curr.next;
        }

        // Check if the element is not present in the list
        if (flag == 1)
            return head;

        // Check if node is only node
        if (curr == head && curr.next == head) {
            head = null;
            return head;
        }

        // If more than one node, check if
        // it is first node
        if (curr == head) {
            prev = head;
            while (prev.next != head)
                prev = prev.next;
            head = curr.next;
            prev.next = head;
        }

        // check if node is last node
        else if (curr.next == head) {
            prev.next = head;
        }
        else {
            prev.next = curr.next;
        }
        return head;
    }

    /* Driver code */
    public static void main(String args[])
    {
        /* Initialize lists as empty */
        Node head = null;

        /* Created linked list will be 2.5.7.8.10 */
        head = push(head, 2);
        head = push(head, 5);
        head = push(head, 7);
        head = push(head, 8);
        head = push(head, 10);

        System.out.printf("List Before Deletion: ");
        printList(head);

        head = deleteNode(head, 7);

        System.out.printf("List After Deletion: ");
        printList(head);
    }
}

// This code is contributed by Susobhan Akhuli
using System;

// Structure for a node
public class Node {
    public int data;
    public Node next;
}

// Class for Circular Linked List
public class CircularLinkedList {

    // Function to insert a node at the
    // beginning of a Circular linked list
    public static void Push(ref Node head_ref, int data)
    {

        // Create a new node and make head
        // as next of it.
        Node ptr1 = new Node();
        ptr1.data = data;
        ptr1.next = head_ref;

        // If linked list is not NULL then
        // set the next of last node
        if (head_ref != null) {

            // Find the node before head and
            // update next of it.
            Node temp = head_ref;
            while (temp.next != head_ref)
                temp = temp.next;
            temp.next = ptr1;
        }
        else

            // For the first node
            ptr1.next = ptr1;

        head_ref = ptr1;
    }

    // Function to print nodes in a given
    // circular linked list
    public static void PrintList(Node head)
    {
        Node temp = head;
        if (head != null) {
            do {
                Console.Write(temp.data + " ");
                temp = temp.next;
            } while (temp != head);
        }

        Console.WriteLine();
    }

    // Function to delete a given node
    // from the list
    public static void DeleteNode(ref Node head, int key)
    {

        // If linked list is empty
        if (head == null)
            return;

        // If the list contains only a
        // single node
        if (head.data == key && head.next == head) {
            head = null;
            return;
        }

        Node last = head, d;

        // If head is to be deleted
        if (head.data == key) {

            // Find the last node of the list
            while (last.next != head)
                last = last.next;

            // Point last node to the next of
            // head i.e. the second node
            // of the list
            last.next = head.next;
            head = last.next;
            return;
        }

        // Either the node to be deleted is
        // not found or the end of list
        // is not reached
        while (last.next != head && last.next.data != key) {
            last = last.next;
        }

        // If node to be deleted was found
        if (last.next.data == key) {
            d = last.next;
            last.next = d.next;
        }
        else
            Console.WriteLine(
                "Given node is not found in the list!!!");
    }

    // Driver code
    public static void Main()
    {

        // Initialize lists as empty
        Node head = null;

        // Created linked list will be
        // 2->5->7->8->10
        Push(ref head, 2);
        Push(ref head, 5);
        Push(ref head, 7);
        Push(ref head, 8);
        Push(ref head, 10);

        Console.Write("List Before Deletion: ");
        PrintList(head);

        DeleteNode(ref head, 7);

        Console.Write("List After Deletion: ");
        PrintList(head);
    }
}
      // Javascript program to delete a given key from linked list.

      // Structure for a node
      class Node {
        constructor() {
          this.data;
          this.next;
        }
      }

      // Function to insert a node at the
      // beginning of a Circular linked list
      function push(head, data) {
        // Create a new node and make head
        // as next of it.
        var ptr1 = new Node();
        ptr1.data = data;
        ptr1.next = head;

        // If linked list is not NULL then
        // set the next of last node
        if (head != null) {
          // Find the node before head and
          // update next of it.
          let temp = head;
          while (temp.next != head) temp = temp.next;
          temp.next = ptr1;
        }

        // For the first node
        else ptr1.next = ptr1;

        head = ptr1;
        return head;
      }

      // Function to print nodes in a given
      // circular linked list
      function printList(head) {
        let tempp = head;
        if (head != null) {
          do {
            console.log(tempp.data);
            tempp = tempp.next;
          } while (tempp != head);
        }
      }

      // Function to delete a given node
      // from the list
      function deleteNode(head, key) {
        // If linked list is empty
        if (head == null) return;

        // If the list contains only a
        // single node
        if (head.data == key && head.next == head) {
          head = null;
          return;
        }

        let last = head;

        // If head is to be deleted
        if (head.data == key) {
          // Find the last node of the list
          while (last.next != head) last = last.next;

          // Point last node to the next of
          // head i.e. the second node
          // of the list
          last.next = head.next;
          head = last.next;
          return;
        }

        // Either the node to be deleted is
        // not found or the end of list
        // is not reached
        while (last.next != head && last.next.data != key) {
          last = last.next;
        }

        // If node to be deleted was found
        if (last.next.data == key) {
          d = last.next;
          last.next = d.next;
          d = null;
        } else console.log("Given node is not found in the list!!!");
      }

      // Driver code

      // Initialize lists as empty
      head = null;

      // Created linked list will be
      // 2->5->7->8->10
      head = push(head, 2);
      head = push(head, 5);
      head = push(head, 7);
      head = push(head, 8);
      head = push(head, 10);

      console.log("List Before Deletion: ");
      printList(head);

      deleteNode(head, 7);

      console.log("List After Deletion: ");
      printList(head);
# Python program to delete a given key from linked list
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

# Function to insert a node at the
# beginning of a Circular linked list


def push(head, data):
    # Create a new node and make head as next of it.
    newP = Node(data)
    newP.next = head

    # If linked list is not NULL then
    # set the next of last node
    if head != None:
        # Find the node before head and
        # update next of it.
        temp = head
        while (temp.next != head):
            temp = temp.next
        temp.next = newP
    else:
        newP.next = newP
    head = newP
    return head

# Function to print nodes in a given circular linked list


def printList(head):
    if head == None:
        print("List is Empty")
        return
    temp = head.next
    print(head.data, end=' ')
    if (head != None):
        while (temp != head):
            print(temp.data, end=" ")
            temp = temp.next
    print()

# Function to delete a given node
# from the list


def deleteNode(head, key):
    # If linked list is empty
    if (head == None):
        return

    # If the list contains only a
    # single node
    if (head.data == key and head.next == head):
        head = None
        return

    last = head

    # If head is to be deleted
    if (head.data == key):
        # Find the last node of the list
        while (last.next != head):
            last = last.next

        # Point last node to the next of
        # head i.e. the second node
        # of the list
        last.next = head.next
        head = last.next
        return

    # Either the node to be deleted is
    # not found or the end of list
    # is not reached
    while (last.next != head and last.next.data != key):
        last = last.next

    # If node to be deleted was found
    if (last.next.data == key):
        d = last.next
        last.next = d.next
        d = None
    else:
        print("Given node is not found in the list!!!")


# Driver code
# Initialize lists as empty
head = None

# Created linked list will be
# 2->5->7->8->10
head = push(head, 2)
head = push(head, 5)
head = push(head, 7)
head = push(head, 8)
head = push(head, 10)

print("List Before Deletion: ")
printList(head)

deleteNode(head, 7)
print("List After Deletion: ")
printList(head)

Output
List Before Deletion: 10 8 7 5 2 
List After Deletion: 10 8 5 2 

Time Complexity: O(N), Worst case occurs when the element to be deleted is the last element and we need to move through the whole list.
Auxiliary Space: O(1), As constant extra space is used.

Advantages of Circular Linked Lists: 

Disadvantages of circular linked list:

Applications of circular linked lists:

Why circular linked list?

Next Posts: Circular Linked List | Set 2 (Traversal) Circular Singly Linked List | Insertion Please write comments if you find any bug in above code/algorithm, or find other ways to solve the same problem

Article Tags :