easyLinked ListPure DSA~14 min
Reverse Playlist Order
A music app stores the current playlist as a singly linked list of tracks. The user taps 'reverse order' — flip the list in place so the last track becomes the head.
Problem
Given the head of a singly linked list, reverse it and return the new head.
Input
The head of a list with n nodes (0 ≤ n ≤ 10^5); shown as the value sequence.
Output
The reversed value sequence.
Constraints
- 0 ≤ n ≤ 100,000
- Reverse in place — O(1) extra space
- Node values fit in a 32-bit int
Examples
Example 1
Input
head = [1, 2, 3, 4, 5]
Output
[5, 4, 3, 2, 1]
Each next pointer is flipped to point at the previous node.
Example 2
Input
head = [1]
Output
[1]
A single node reverses to itself.