Assuming that the linked list contains nodes with integer values, we can find the range of all nodes in the linked list using the following algorithm:
min
and max
, both to the value of the first node in the list.min
, update min
to the value of the node.
b. If the value of the node is greater than max
, update max
to the value of the node.max
and min
.Here is an implementation of this algorithm in Swift:
main.swift478 chars19 lines
This function takes a LinkedList<Int>
as input, and returns the range of all nodes in the list as an Int
. If the list is empty, it returns nil
.
gistlibby LogSnag