Le migliori 150 intervisteFacile

Rimuovi l'ennesimo nodo dalla fine dell'elenco

Guida dettagliata e implementazione Python per il problema "Rimuovi l'ennesimo nodo dalla fine dell'elenco".

Dichiarazione del problema

Facile

Data la testa di una lista concatenata, rimuovi l'ennesimo nodo dalla fine della lista e restituisci la sua testa.

L'elenco collegato è rappresentato come un elenco Python. Implementa una funzione removeNthFromEnd(head: list, n: int) -> list che restituisca l'elenco dopo la rimozione.

Vincoli
  • The number of nodes in the list is sz
  • 1 <= sz <= 30
  • 0 <= Node.val <= 100
  • 1 <= n <= sz

Esempi

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

The 2nd node from the end is 4. After removing it, the list becomes 1->2->3->5.

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

There is only one node and we remove it, so the list becomes empty.

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

The 1st node from the end is 2. After removing it, the list becomes [1].

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.