# Python program to find the maximum and minimum value node from a doubly linked list

PythonServer Side ProgrammingProgramming

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.

Below is a demonstration for the same −

## Example

Live Demo

class Node:
def __init__(self, my_data):
self.prev = None
self.data = my_data
self.next = None
class double_list:
def __init__(self):
self.tail = None
new_node = Node(my_data)
self.tail.next = None
else:
self.tail.next = new_node
new_node.previous = self.tail
self.tail = new_node
self.tail.next = None
def min_node(self):
print("The list is empty")
return 0
else:
while(curr != None):
if(min > curr.data):
min = curr.data
curr = curr.next
return min
def max_node(self):
print("The list is empty")
return 0
else:
while(curr != None):
if(curr.data > max):
max = curr.data
curr = curr.next
return max
def print_it(self):
print("The list is empty")
return
print("The nodes in the doubly linked list are :")
while curr != None:
print(curr.data)
curr = curr.next
my_instance = double_list()
my_instance.print_it()
print("The node with maximum value is : ")
print(my_instance.max_node())
print("The node with minimum value is : ")
print(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

## Explanation

• The ‘Node’ class is created.
• Another class with required attributes is created.
• A method named ‘add_data’ is defined, that is used to add data to the doubly linked list.
• Another method named ‘print_it’ is defined, that displays the nodes of the circular linked list.
• Another method named ‘max_node’ is defined, that searches for the maximum value in a doubly linked list.
• Another method named ‘min_node’ is defined, that searches for the minimum value in a doubly linked list.
• An object of the ‘double_list’ class is created, and the methods are called on it to find the minimum and maximum values of a node in the doubly linked list.
• An ‘init’ method is defined, that the root, head, and tail nodes of the doubly linked list to None.
• The list is iterated over, and the maximum and minimum values are found.
• This is displayed on the console using the ‘print_it’ method.