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 doubly linked list
When it is required to find the maximum and minimum values from a doubly linked list, a Node class needs to be created. In this class, there are three attributes: the data that is present in the node, the access to the next node of the linked list, and the access to the previous node of the linked list.
A doubly linked list allows traversal in both directions, making it easy to find minimum and maximum values by iterating through all nodes once.
Implementation
Below is a complete implementation that creates a doubly linked list and finds the maximum and minimum values ?
class Node:
def __init__(self, my_data):
self.prev = None
self.data = my_data
self.next = None
class DoublyLinkedList:
def __init__(self):
self.head = None
self.tail = None
def add_data(self, my_data):
new_node = Node(my_data)
if self.head == None:
self.head = self.tail = new_node
self.head.prev = None
self.tail.next = None
else:
self.tail.next = new_node
new_node.prev = self.tail
self.tail = new_node
self.tail.next = None
def min_node(self):
curr = self.head
if self.head == None:
print("The list is empty")
return None
else:
min_val = self.head.data
while curr != None:
if min_val > curr.data:
min_val = curr.data
curr = curr.next
return min_val
def max_node(self):
curr = self.head
if self.head == None:
print("The list is empty")
return None
else:
max_val = self.head.data
while curr != None:
if curr.data > max_val:
max_val = curr.data
curr = curr.next
return max_val
def print_it(self):
curr = self.head
if self.head == None:
print("The list is empty")
return
print("The nodes in the doubly linked list are:")
while curr != None:
print(curr.data)
curr = curr.next
# Create instance and test
my_instance = DoublyLinkedList()
print("Elements are being added to the doubly linked list")
my_instance.add_data(10)
my_instance.add_data(24)
my_instance.add_data(54)
my_instance.add_data(77)
my_instance.add_data(92)
my_instance.print_it()
print("The node with maximum value is:", my_instance.max_node())
print("The node with minimum value is:", my_instance.min_node())
Output
Elements are being added to the doubly linked list The nodes in the doubly linked list are: 10 24 54 77 92 The node with maximum value is: 92 The node with minimum value is: 10
How It Works
The algorithm works by traversing the entire doubly linked list once for each operation:
- Node Class: Contains data and pointers to both previous and next nodes
- add_data(): Adds new nodes at the tail of the list
- min_node(): Traverses the list and keeps track of the minimum value found
- max_node(): Traverses the list and keeps track of the maximum value found
- print_it(): Displays all nodes in the list from head to tail
Key Features
- Time Complexity: O(n) for both min and max operations
- Space Complexity: O(1) additional space for the search operations
- Edge Case Handling: Returns None for empty lists
- Single Pass: Each operation requires only one traversal of the list
Conclusion
Finding minimum and maximum values in a doubly linked list requires a single traversal with O(n) time complexity. The doubly linked structure provides flexibility for bidirectional traversal, though only forward traversal is needed for min/max operations.
