Sezione DSAFacile

Piega elenco collegato

Guida dettagliata e implementazione Python per il problema "Piega elenco collegato".

Dichiarazione del problema

Facile

Scrivi una funzione fold_list(arr) che rappresenta un elenco collegato singolarmente come un elenco Python arr e restituisce l'elenco piegato. Piegando un elenco L0 -> L1 -> ... -> Ln-1 -> Ln si ottiene L0 -> Ln -> L1 -> Ln-1 -> L2 -> Ln-2 ....

Vincoli
  • 0 <= len(arr) <= 1000
  • -1000 <= arr[i] <= 1000

Esempi

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

The nodes are reordered by interleaving the first, last, second, second-to-last nodes, etc.

Example 2
Input
arr = [1, 2, 3, 4]
Output
[1, 4, 2, 3]
Need a Hint?
Prendi in considerazione l'utilizzo di strutture dati specifiche degli elenchi collegati 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.