Le migliori 150 intervisteFacile

Elenco collegato inverso

Guida dettagliata e implementazione Python per il problema della "Lista collegata inversa".

Dichiarazione del problema

Facile

Data l'inizio di una lista concatenata singolarmente, inverte la lista e restituisce la lista invertita.

L'elenco collegato è rappresentato come un elenco di valori. Implementa una funzione reverseList(head: list) -> list che accetta un elenco che rappresenta l'elenco collegato e restituisce l'elenco invertito.

Vincoli
  • The number of nodes in the list is in the range [0, 5000]
  • -5000 <= Node.val <= 5000

Esempi

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

The original list is 1->2->3->4->5. After reversing, it becomes 5->4->3->2->1.

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

The original list is 1->2. After reversing, it becomes 2->1.

Example 3
Input
[]
Output
[]
Explanation

An empty list reversed is still an empty list.

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.