Python program to insert a new node at the beginning of the Circular Linked List

When it is required to insert a new node at the beginning of a circular linked list, a 'Node' class needs to be created. In this class, there are two attributes, the data that is present in the node, and the access to the next node of the linked list.

In a circular linked list, the head and the rear are adjacent to each other. They are connected to form a circle, and don't have 'NULL' value in the last node.

Another class needs to be created that would have an initialization function, and the head of the node would be initialized to 'None'. 

Multiple methods are defined by the user to add node at the beginning of the linked list, and to print the node values.

Below is a demonstration for the same −


Live Demo

class Node:  
   def __init__(self,data): = data = None  
class list_creation:  
   def __init__(self):  
      self.head = Node(None)  
      self.tail = Node(None) = self.tail = self.head  

   def add_at_beginning(self,my_data):  
      new_node = Node(my_data);  
      if is None:  
       self.head = new_node;  
       self.tail = new_node; = self.head;  
         temp = self.head;   = temp;  
         self.head = new_node;   = self.head;
   def print_it(self):  
      curr = self.head;  
      if self.head is None:  
         print("The list is empty");  
         while( != self.head):  
            curr =;  
class circular_linked_list:  
   my_cl = list_creation()
   print("Values are being added to the list")


Values are being added to the list




  • The 'Node' class is created.
  • Another class with required attributes is created.
  • Another method named 'add_at_beginning' is defined, that is used to add data to the circular linked list at the beginning, i.e before the 'head' node.
  • Another method named 'print_it' is defined, that displays the nodes of the circular linked list.
  • An object of the 'list_creation' class is created, and the methods are called on it to add data.
  • An 'init' method is defined, that the first and last nodes of the circular linked list to None.
  • The 'add_at_beginning' method is called.
  • It gets the head of the linked list, add an element before it, and references its address to the tail pointer and to the next pointer.
  • This is displayed on the console using the 'print_it' method.

Updated on: 13-Mar-2021


Kickstart Your Career

Get certified by completing the course

Get Started