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

python tutorials and learn python

Created with Sketch.

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

In this program, we will create a circular linked list and delete a node from the middle of the list. If the list is empty, display the message “List is empty”. If the list is not empty, we will calculate the size of the list and then divide it by 2 to get the mid-point of the list. We maintain two pointers temp and current. Current will point to the previous node of temp. We will iterate through the list until mid-point is reached then the current will point to the middle node. We delete middle node such that current’s next will be temp’s next node.

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

Circular linked list after deleting the node from the middle of the list

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

Consider the above list. Size of the list is 4. Mid-point of the node is 2. To remove C from the list, we will iterate through the list till mid-point. Now current will point to B and temp will point to C. C will be removed when B will point to D.

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 a variable size and two methods: deleteMid() and display() .
  3. deleteMid() will delete the node from the middle 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, it will calculate the size of the list. Divide the size by 2 and store it in the variable count.
  • Temp will point to head, and current will point to the previous node to temp.
  • Iterate the list till current will point middle node of the list.
  • Current will point to node next to temp, i.e., removes the node next to current.

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.         self.size = 0;
  14.     #This function will add the new node at the end of the list.  
  15.     def add(self,data):
  16.         newNode = Node(data);
  17.         #Checks if the list is empty.  
  18.         if self.head.data is None:
  19.             #If list is empty, both head and tail would point to new node.  
  20.             self.head = newNode;
  21.             self.tail = newNode;
  22.             newNode.next = self.head;
  23.         else:
  24.             #tail will point to new node.  
  25.             self.tail.next = newNode;
  26.             #New node will become new tail.  
  27.             self.tail = newNode;
  28.             #Since, it is circular linked list tail will point to head.  
  29.             self.tail.next = self.head;
  30.         #Size will count the number of nodes in the list  
  31.         self.size = int(self.size)+1;
  32.     #Deletes node from the middle of the list  
  33.     def deleteMid(self):
  34.         #Checks whether list is empty  
  35.         if(self.head == None):
  36.             return;
  37.         else:
  38.             #Store the mid position of the list  
  39.             count = (self.size//2if (self.size % 2 == 0else ((self.size+1)//2);
  40.             #Checks whether head is equal to tail or not, if yes then list has only one node.  
  41.             ifself.head != self.tail ):
  42.                 #Initially temp will point to head;  
  43.                 temp = self.head;
  44.                 current = None;
  45.                 #Current will point to node previous to temp  
  46.                 #If temp is pointing to node 2 then current will points to node 1.  
  47.                 for i in range(0, count-1):
  48.                     current = temp;
  49.                     temp = temp.next;
  50.                 if(current != None):
  51.                     #temp is the middle that needs to be removed.  
  52.                     #So, current node will point to node next to temp by skipping temp.  
  53.                     current.next = temp.next;
  54.                     #Delete temp;  
  55.                     temp = None;
  56.                 #Current points to null then head and tail will point to node next to temp.  
  57.                 else:
  58.                     self.head = self.tail = temp.next;
  59.                     self.tail.next = self.head;
  60.                     #Delete temp;  
  61.                     temp = None;
  62.             #If the list contains only one element   
  63.             #then it will remove it and both head and tail will point to null  
  64.             else:
  65.                 self.head = self.tail = None;
  66.         self.size = self.size – 1;
  67.     #Displays all the nodes in the list  
  68.     def display(self):
  69.         current = self.head;
  70.         if self.head is None:
  71.             print(“List is empty”);
  72.             return;
  73.         else:
  74.             #Prints each node by incrementing pointer.  
  75.             print(current.data),
  76.             while(current.next != self.head):
  77.                 current = current.next;
  78.                 print(current.data),
  79.             print(“\n”);
  80. class CircularLinkedList:
  81.     cl = CreateList();
  82.     #Adds data to the list  
  83.     cl.add(1);
  84.     cl.add(2);
  85.     cl.add(3);
  86.     cl.add(4);
  87.     #Printing original list  
  88.     print(“Original List:”);
  89.     cl.display();
  90.     while(cl.head != None):
  91.         cl.deleteMid();
  92.         #Printing updated list  
  93.         print(“Updated List:”);
  94.         cl.display();

Output:

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

Leave a Reply

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