150 najlepszych wywiadówŁatwe

Odwróć listę powiązaną

Szczegółowy przewodnik i implementacja Python dla problemu „Odwrócona lista połączona”.

Oświadczenie o problemie

Łatwe

Mając nagłówek listy z pojedynczym łączem, odwróć listę i zwróć odwróconą listę.

Połączona lista jest reprezentowana jako lista wartości. Zaimplementuj funkcję reverseList(head: list) -> list, która pobiera listę reprezentującą listę połączoną i zwraca listę odwróconą.

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

Przykłady

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