Reverse Linked List - Problem
Given the head of a singly linked list, reverse the list, and return the reversed list.
A singly linked list is a data structure where each node contains a value and a reference to the next node in the sequence. Reversing the list means changing the direction of all pointers so that the last node becomes the first, and the first node becomes the last.
Input & Output
Example 1 — Basic List
$
Input:
head = [1,2,3,4,5]
›
Output:
[5,4,3,2,1]
💡 Note:
The linked list 1→2→3→4→5 becomes 5→4→3→2→1 after reversing all the pointer directions.
Example 2 — Two Nodes
$
Input:
head = [1,2]
›
Output:
[2,1]
💡 Note:
Simple case with two nodes: 1→2 becomes 2→1.
Example 3 — Empty List
$
Input:
head = []
›
Output:
[]
💡 Note:
An empty list remains empty after reversal.
Constraints
- The number of nodes in the list is the range [0, 5000]
- -5000 ≤ Node.val ≤ 5000
Visualization
Tap to expand
Understanding the Visualization
1
Input
Original linked list: 1→2→3→4→5
2
Process
Reverse all pointer directions
3
Output
Reversed linked list: 5→4→3→2→1
Key Takeaway
🎯 Key Insight: Use three pointers to safely reverse each link without losing the rest of the list
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code