Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

705
Visualizações
Given an unsorted linked list, find the element with the largest value. (JavaScript)

I'm new to coding and learning Linked List problems. I found a solution but don't really understand what they do. what does (!node.next) mean? Is it the same as node.next != null?

Also, I don't understand this line return (node.value > biggestValueInRest ? node.value : biggestValueInRest);

Does it mean that if node.value is greater than biggestValueInRest, then node.value = biggestValueInRest?

Here is the solution

function findMax(node) {
  if (!node.next) {
    return node.value;
  } else {
    const biggestValueInRest = findMax(node.next);

    return (node.value > biggestValueInRest ? node.value
      : biggestValueInRest);
  }
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

what does (!node.next) mean? Is it the same as node.next != null?

The ! is the negation operator, it negates the value placed in front of it.

for example !node.next is the same as node.next == false.

In this case, !node.next will return true if node.next is a falsy value like false or null or undefined.

In the if statement of your code, it will return the current node's value if there's no next node linked to that node since, in linked lists, nodes contain a value and a link to the next node.

Also, I don't understand this line return (node.value > biggestValueInRest ? node.value : biggestValueInRest);

Does it mean that if node.value is greater than biggestValueInRest, then node.value = biggestValueInRest?

That's a ternary operator, it's like a shortened if that returns a value.

return (node.value > biggestValueInRest ? node.value : biggestValueInRest);

would be the same as:

if (node.value > biggestValueInRest)
  return node.value;
else
  return biggestValueInRest;

The syntax of the ternary operator is basically:

"condition" ? "value to return if true" : "value to return if false"

And to fully answer your question.

I found a solution but don't really understand what they do.

The solution provided is a recursive one, it will call itself for each node in the linked lists until it reaches the final one ( the one which's node.next is empty ), and once that's done, it will return the biggest value of each comparison from there.

Recursive functions aren't always a good practice, since their time complexity can escalate depending on how the developer implemented it.

In this case, it would be O(n) which means that it's linear, which is not too bad for a recursive function.

This means that the time the function will take to finish is affected in proportion to the number of items n the linked list has.

So if it takes 1s per node, then 100 nodes would be 100s for the function to finish.

about 4 years ago · Juan Pablo Isaza Relatório

0

It would have been simpler in python but what you can do is create a function that is looks something like this, will explain later

assuming the list consist only of numeric values

Function(ListItems) {
 Let biggest_val = 0;

 For (let i = 0; i < ListItems.length; i++) {
   If (LisItems[i] > biggest_val) {
  biggest_val = ListItems[i]
}
 }
}

We are first initialising a variable to hold the biggest value of the list for us, and then creating a for loop to loop through the list and check if the current value is greater than our current biggest value if so update the value of our biggest val.

the node.value > biggestValueInRest ? node.value : biggestValueInRest is called a [turnary operator][1] it is a short hand if statement but since you're just beginning it is better to stick with the traditional one

[1] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda