find the range of all nodes in a linked list in python

Here's a function that takes the head node of a linked list and returns the range of the values of all its nodes:

main.py
class Node:
    def __init__(self, value):
        self.value = value
        self.next = None
 
def range_of_linked_list(head):
    if head is None:
        return None
    
    min_val = head.value
    max_val = head.value
    
    current = head
    while current is not None:
        if current.value < min_val:
            min_val = current.value
        elif current.value > max_val:
            max_val = current.value
        current = current.next
 
    return (min_val, max_val)
489 chars
22 lines

This function iterates through each node in the linked list and keeps track of the current minimum and maximum values seen so far. It returns the tuple containing the minimum and maximum values as the range of the linked list.

gistlibby LogSnag