150 principais entrevistasFácil

Remover o enésimo nó do final da lista

Guia detalhado e implementação de Python para o problema 'Remover o enésimo nó do final da lista'.

Declaração do problema

Fácil

Dado o cabeçalho de uma lista encadeada, remova o enésimo nó do final da lista e retorne seu cabeçalho.

A lista vinculada é representada como uma lista Python. Implemente uma função removeNthFromEnd(head: list, n: int) -> list que retorne a lista após a remoção.

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

Exemplos

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?
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.