Python program to delete a node from the beginning of the Circular Linked List

python tutorials and learn python

Created with Sketch.

Python program to delete a node from the beginning of the Circular Linked List

In this program, we will create a circular linked list and delete a node from the beginning of the list. If the list is empty, print the message “List is empty”. If the list is not empty, we will make the head to point to the next node in the list, i.e., we will delete the first node.

Python program to delete a node from the beginning of the Circular Linked List

Circular linked list after deleting the node from the beginning.

Python program to delete a node from the beginning of the Circular Linked List

Here, A represents the head of the list. We need to delete a node from the beginning of the list. So, we will remove A such that B will become new head and tail will point to the new head.

ALGORITHM:

  1. Define a Node class which represents a node in the list. It has two properties data and next which will point to the next node.
  2. Define another class for creating the circular linked list, and it has two nodes: head and tail. It has two methods: deleteStart() and display() .
  3. deleteStart() will delete the node from the beginning of the list:
  • It first checks whether the head is null (empty list) then, it will return from the function as there is no node present in the list.
  • If the list is not empty, it will check whether the list has only one node.
  • If the list has only one node, it will set both head and tail to null.
  • If the list has more than one node then, the head will point to the next node in the list that is, we will remove the previous head node and tail will point to the new head.

a. display() will show all the nodes present in the list.

  • Define a new node ‘current’ that will point to the head.
  • Print current.data till current will points to head again.
  • Current will point to the next node in the list in each iteration.

PROGRAM:

  1. #Represents the node of list.  
  2. class Node:
  3.     def __init__(self,data):
  4.         self.data = data;
  5.         self.next = None;
  6. class CreateList:
  7.     #Declaring head and tail pointer as null.  
  8.     def __init__(self):
  9.         self.head = Node(None);
  10.         self.tail = Node(None);
  11.         self.head.next = self.tail;
  12.         self.tail.next = self.head;
  13.     #This function will add the new node at the end of the list.  
  14.     def add(self,data):
  15.         newNode = Node(data);
  16.         #Checks if the list is empty.  
  17.         if self.head.data is None:
  18.             #If list is empty, both head and tail would point to new node.  
  19.             self.head = newNode;
  20.             self.tail = newNode;
  21.             newNode.next = self.head;
  22.         else:
  23.             #tail will point to new node.  
  24.             self.tail.next = newNode;
  25.             #New node will become new tail.  
  26.             self.tail = newNode;
  27.             #Since, it is circular linked list tail will point to head.  
  28.             self.tail.next = self.head;
  29.     #Deletes node from the beginning of the list  
  30.     def deleteStart(self):
  31.         #Checks whether list is empty  
  32.         if(self.head == None):
  33.             return;
  34.         else:
  35.             #Checks whether contain only one element  
  36.             #If not, head will point to next element in the list and tail will point to new head.  
  37.             if(self.head != self.tail ):
  38.                 self.head = self.head.next;
  39.                 self.tail.next = self.head;
  40.             #If the list contains only one element   
  41.             #then it will remove it and both head and tail will point to null  
  42.             else:
  43.                 self.head = self.tail = None;
  44.     #Displays all the nodes in the list  
  45.     def display(self):
  46.         current = self.head;
  47.         if self.head is None:
  48.             print(“List is empty”);
  49.             return;
  50.         else:
  51.             #Prints each node by incrementing pointer.  
  52.             print(current.data, end= ‘ ‘);
  53.             while(current.next != self.head):
  54.                 current = current.next;
  55.                 print(current.data,end=‘ ‘);
  56.             print(“\n”);
  57. class CircularLinkedList:
  58.     cl = CreateList();
  59.     #Adds data to the list  
  60.     cl.add(1);
  61.     cl.add(2);
  62.     cl.add(3);
  63.     cl.add(4);
  64.     #Printing original list  
  65.     print(“Original List:”);
  66.     cl.display();
  67.     while(cl.head != None):
  68.         cl.deleteStart();
  69.         #Printing updated list  
  70.         print(“Updated List:”);
  71.         cl.display();

Output:

Original List: 
 1 2 3 4
Updated List: 
 2 3 4
Updated List: 
 3 4
Updated List: 
 4
Updated List: 
List is empty

Leave a Reply

Your email address will not be published. Required fields are marked *