Top 150 InterviewEasy

Reverse Linked List

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

Problem Statement

Easy

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.

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

Examples

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

Ready to Solve?

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

Open in Editor

Recommended Python Resources

Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.