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

To find the range of all nodes in a linked list, you need to iterate through each node and keep track of the minimum and maximum values. Here's an example implementation in C#:

main.cs
public static Tuple<int, int> FindRange(LinkedList<int> list)
{
    if (list == null || list.First == null)
    {
        return null;
    }

    int min = list.First.Value;
    int max = list.First.Value;

    foreach (int value in list)
    {
        if (value < min)
        {
            min = value;
        }

        if (value > max)
        {
            max = value;
        }
    }

    return Tuple.Create(min, max);
}
430 chars
26 lines

In this implementation, we use a Tuple<int, int> to return both the minimum and maximum values as a pair. We first check if the list is empty or null, and if so we return null. Otherwise, we initialize our minimum and maximum values to the value of the first node.

We then iterate through each node in the list, checking if the current node's value is less than our current minimum value or greater than our current maximum value. If it is, we update our minimum or maximum accordingly.

Finally, we return our minimum and maximum values as a Tuple<int, int>.

gistlibby LogSnag