Le migliori 150 intervisteFacile

Unisci due elenchi ordinati

Guida dettagliata e implementazione Python per il problema "Unisci due elenchi ordinati".

Dichiarazione del problema

Facile

Ti vengono fornite le teste di due elenchi collegati ordinati lista1 e lista2. Unisci i due elenchi in un unico elenco ordinato. La lista dovrebbe essere realizzata unendo insieme i nodi delle prime due liste. Restituisce l'inizio dell'elenco collegato unito.

Gli elenchi collegati sono rappresentati come elenchi Python. Implementa una funzione mergeTwoLists(list1: list, list2: list) -> list che restituisce l'elenco ordinato unito.

Vincoli
  • 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

Esempi

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?
Prendi in considerazione l'utilizzo di strutture dati specifiche dell'elenco collegato come set o heap.
Edge Cases to Watch
  • Strutture di input vuote
  • Ingressi a elemento singolo
  • Grandi limiti numerici

Pronto a risolvere?

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

Apri nell'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

Risorse Python consigliate

Espandi le tue conoscenze con tutorial interattivi, foglietti illustrativi e confronti di codici correlati.