150 najlepszych wywiadówŁatwe

Usuń N-ty węzeł z końca listy

Szczegółowy przewodnik i implementacja Python dla problemu „Usuń N-ty węzeł z końca listy”.

Oświadczenie o problemie

Łatwe

Mając nagłówek połączonej listy, usuń n-ty węzeł z końca listy i zwróć jego głowę.

Połączona lista jest reprezentowana jako lista Python. Zaimplementuj funkcję removeNthFromEnd(head: list, n: int) -> list, która zwraca listę po usunięciu.

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

Przykłady

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?
Rozważ użycie struktur danych specyficznych dla listy połączonej, takich jak zestawy lub sterty.
Edge Cases to Watch
  • Puste struktury wejściowe
  • Wejścia jednoelementowe
  • Duże granice liczbowe

Gotowy do rozwiązania?

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

Otwórz w Edytorze
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

Polecane zasoby Pythona

Poszerzaj swoją wiedzę dzięki powiązanym interaktywnym samouczkom, ściągawkom i porównaniom kodów.