Top 150 Interview簡単

Reverse Linked List

Detailed guide and Python implementation for the 'Reverse Linked List' problem.

問題提起

簡単

Given the head of a singly linked list, reverse the list, and return the reversed list.

The linked list is represented as a list of values. Implement a function reverseList(head: list) -> list that takes a list representing the linked list and returns the reversed list.

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

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?
Consider using Linked List-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

解決する準備はできましたか?

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

エディタで開く
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

推奨される Python リソース

関連するインタラクティブなチュートリアル、チートシート、コード比較で知識を深めてください。