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

To find the range of all nodes in a linked list in JavaScript, we can iterate through the entire list and keep track of the minimum and maximum values. Here's a code implementation of the algorithm:

index.tsx
function findRange(head) {
  if (head === null) {
    return null;
  }
  
  let min = head.value;
  let max = head.value;
  let current = head.next;
  
  while (current !== null) {
    if (current.value < min) {
      min = current.value;
    }
    else if (current.value > max) {
      max = current.value;
    }
    current = current.next;
  }
  
  return [min, max];
}
372 chars
22 lines

In this code, we start by setting the minimum and maximum values to the value of the head node. We then iterate through the rest of the nodes in the list, checking each node's value to see if it's the new minimum or maximum. After we've iterated through the entire list, we return an array of the minimum and maximum values.

This algorithm has a time complexity of O(n), where n is the number of nodes in the list.

gistlibby LogSnag