Reverse a singly linked list so the last node becomes the head and all next pointers are flipped.
Problem
Given the head of a singly linked list, reverse the list in place and return the new head.
After reversal, the node order should be completely flipped: the original tail becomes the new head, and every node's next pointer should point to its previous node.
You should aim to solve this by rearranging pointers rather than creating a second list.
Input Format
- A singly linked list given by its head node.
- Each node contains an integer value and a reference to the next node.
Output Format
- Return the head node of the reversed linked list.
Constraints
- The list may be empty.
- The list contains a finite number of nodes.
- The reversal should be done in time.
- Use extra space if possible.
Hints
- Keep track of the previous node, the current node, and the next node before rewiring pointers.
- An iterative approach is usually the clearest way to reverse the links safely.
- If you try recursion, think carefully about how the new head is returned from the deepest call.
Input Format
head: the head of a singly linked list
Output Format
- The head of the reversed linked list
Constraints
- nodes
- Run in time
- Prefer extra space
Example 1
Input
head = [1,2,3,4,5]
Output
[5,4,3,2,1]
Explanation
The list is reversed so the tail becomes the new head.
Example 2
Input
head = [1,2]
Output
[2,1]
Explanation
Each next pointer is flipped once.
Show 1 more example
Example 3
Input
head = []
Output
[]
Explanation
An empty list remains empty after reversal.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.