Introduction to Circular Linked List

A circular linked list is a data structure where the last node connects back to the first, forming a loop. This structure allows for continuous traversal without any interruptions. Circular linked lists are especially helpful for tasks like scheduling and managing playlists, this allowing for smooth navigation. In this tutorial, we’ll cover the basics of circular linked lists, how to work with them, their advantages and disadvantages, and their applications.

What is a Circular Linked List?

A circular linked list is a special type of linked list where all the nodes are connected to form a circle. Unlike a regular linked list, which ends with a node pointing to NULL , the last node in a circular linked list points back to the first node. This means that you can keep traversing the list without ever reaching a NULL value.

Types of Circular Linked Lists

We can create a circular linked list from both singly linked lists and doubly linked lists . So, circular linked list are basically of two types:

1. Circular Singly Linked List

In Circular Singly Linked List , each node has just one pointer called the “ next ” pointer. The next pointer of last node points back to the first node and this results in forming a circle. In this type of Linked list we can only move through the list in one direction.

Representation-of-circular-linked-list

Representation of Circular Singly Linked List

2. Circular Doubly Linked List:

In circular doubly linked list, each node has two pointers prev and next, similar to doubly linked list. The prev pointer points to the previous node and the next points to the next node. Here, in addition to the last node storing the address of the first node, the first node will also store the address of the last node .

Representation-of-circular-doubly-linked-list

Representation of Circular Doubly Linked List

Note: In this article, we will use the circular singly linked list to explain the working of circular linked lists.

Representation of a Circular Singly Linked List

Let’s take a look on the structure of a circular linked list.

Node-structure-of-circular-linked-list

Representation of a Circular Singly Linked List

Create/Declare a Node of Circular Linked List

Syntax to Declare a Circular Linked List in Different Languages:

                                 Java
 
                   JavaScript

In the code above, each node has data and a pointer to the next node. When we create multiple nodes for a circular linked list, we only need to connect the last node back to the first one.

Example of Creating a Circular Linked List

Here’s an example of creating a circular linked list with three nodes (2, 3, 4):

Circular-Linked-List

Created a circular linked list with 3 nodes

                                   Java
                   Python
                   JavaScript


In the above code, we have created three nodes first, second, and last having values 2, 3, and 4 respectively.

Why have we taken a pointer that points to the last node instead of the first node?

For the insertion of a node at the beginning, we need to traverse the whole list. Also, for insertion at the end, the whole list has to be traversed. If instead of the start pointer, we take a pointer to the last node, then in both cases there won’t be any need to traverse the whole list. So insertion at the beginning or at the end takes constant time, irrespective of the length of the list.

Operations on the Circular Linked list:

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