Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python program to find the maximum and minimum value node from a circular linked list
When it is required to find the maximum and minimum node values from a circular linked list, a Node class needs to be created. In this class, there are two attributes: the data present in the node, and the access to the next node of the linked list.
In a circular linked list, the head and the tail are adjacent to each other. They are connected to form a circle, and don't have None value in the last node − instead, the last node points back to the first node.
Implementation
The implementation involves creating a Node class and a CircularLinkedList class with methods to add data and find minimum/maximum values ?
class Node:
def __init__(self, data):
self.data = data
self.next = None
class CircularLinkedList:
def __init__(self):
self.head = None
def add_data(self, data):
new_node = Node(data)
if self.head is None:
self.head = new_node
new_node.next = self.head
else:
current = self.head
while current.next != self.head:
current = current.next
current.next = new_node
new_node.next = self.head
def find_min_node(self):
if self.head is None:
print("The list is empty")
return None
current = self.head
min_val = self.head.data
while True:
if current.data < min_val:
min_val = current.data
current = current.next
if current == self.head:
break
print("Minimum value node in the list:", min_val)
return min_val
def find_max_node(self):
if self.head is None:
print("The list is empty")
return None
current = self.head
max_val = self.head.data
while True:
if current.data > max_val:
max_val = current.data
current = current.next
if current == self.head:
break
print("The maximum value node is:", max_val)
return max_val
# Example usage
my_cl = CircularLinkedList()
print("Adding values to the circular linked list...")
my_cl.add_data(11)
my_cl.add_data(52)
my_cl.add_data(36)
my_cl.add_data(74)
my_cl.find_max_node()
my_cl.find_min_node()
Adding values to the circular linked list... The maximum value node is: 74 Minimum value node in the list: 11
How It Works
The algorithm works by traversing the circular linked list once and comparing each node's data with the current minimum and maximum values:
- Node Class: Contains data and a pointer to the next node
- CircularLinkedList Class: Manages the circular structure where the last node points back to the first
- add_data(): Adds new nodes while maintaining the circular property
- find_min_node(): Traverses the list to find the minimum value
- find_max_node(): Traverses the list to find the maximum value
Time and Space Complexity
- Time Complexity: O(n) for both finding minimum and maximum, where n is the number of nodes
- Space Complexity: O(1) as we only use a constant amount of extra space
Conclusion
Finding minimum and maximum values in a circular linked list requires a single traversal while carefully handling the circular nature. The key is to stop when we return to the head node to avoid infinite loops.
