150 principais entrevistasFácil

Mesclar duas listas classificadas

Guia detalhado e implementação de Python para o problema 'Mesclar duas listas classificadas'.

Declaração do problema

Fácil

Você recebe os cabeçalhos de duas listas vinculadas classificadas, lista1 e lista2. Mesclar as duas listas em uma lista classificada. A lista deve ser feita unindo os nós das duas primeiras listas. Retorne o cabeçalho da lista vinculada mesclada.

As listas vinculadas são representadas como listas Python. Implemente uma função mergeTwoLists(list1: list, list2: list) -> list que retorna a lista classificada mesclada.

Restrições
  • The number of nodes in both lists is in the range [0, 50]
  • -100 <= Node.val <= 100
  • Both list1 and list2 are sorted in non-decreasing order

Exemplos

Example 1
Input
[1,2,4], [1,3,4]
Output
[1,1,2,3,4,4]
Explanation

Merging 1->2->4 and 1->3->4 gives 1->1->2->3->4->4.

Example 2
Input
[], []
Output
[]
Explanation

Both lists are empty, so the merged list is also empty.

Example 3
Input
[], [0]
Output
[0]
Explanation

Merging an empty list with [0] gives [0].

Need a Hint?
Considere usar estruturas de dados específicas de listas vinculadas, como conjuntos ou heaps.
Edge Cases to Watch
  • Estruturas de entrada vazias
  • Entradas de elemento único
  • Grandes limites numéricos

Pronto para resolver?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Abrir no Editor
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

Recursos Python recomendados

Expanda seu conhecimento com tutoriais interativos relacionados, folhas de dicas e comparações de código.