Reverse the Playlist
A music app stores each playlist as a chain of songs. Every song knows only which song comes next; there is no way to jump back.
A listener taps "Play in reverse". The app must flip the chain in place, so every song points to the one that used to come before it.
Your task
Write reversePlaylist(head). head is the first node of a singly linked list (each node has val and next). Reverse the list and return the new first node.
An empty playlist is null (None in Python). Lists are shown as arrays like [3, 8, 1, 9]; your function receives and returns real nodes.
reversePlaylist(head: ListNode) → ListNode
Examples
head = [3, 8, 1, 9]
[9, 1, 8, 3]
3 → 8 → 1 → 9 with every arrow flipped is 9 → 1 → 8 → 3. The new head is 9.
head = []
[]
An empty playlist stays empty. Return null (None in Python).
Constraints
- 0 ≤ number of songs ≤ 5,000
- Song ids are integers.
- Reverse the links; do not just create a reversed copy of the values (though that also passes the tests, it misses the point).
Start with brute force
Copy every value into an array, then build a new list from the array backwards.
That takes O(n) time but also O(n) extra memory, and it avoids the real skill, which is rewiring pointers without losing your place. The in-place version needs only O(1) extra memory.
Hints
0 of 4 shown- Hint 1
Take 8 in
3 → 8 → 1 → 9. After reversing, where should itsnextpoint? - Hint 2
Each
nextmust point to the node before it, but a node does not know its predecessor. You must carry that as you walk forward. - Hint 3
Keep
prev(starting at null) for the node you just visited. Changingcurrent.nextloses your only way forward. What must you save first? - Hint 4
Walk with
prev,currentandnext. Each step: savenext = current.next, setcurrent.next = prev, thenprev = current,current = next. Whencurrentis null,previs the new head.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
Flipping current.next destroys your only link to the rest of the list, so save it first.
Trace 3 → 8 → 1:
- prev = null, current = 3. Save next = 8. Point 3 → null. Now prev = 3, current = 8.
- Save next = 1. Point 8 → 3. Now prev = 8, current = 1.
- Save next = null. Point 1 → 8. Now prev = 1, current = null.
The list reads 1 → 8 → 3 → null, and prev is its head. For an empty list the loop never runs and you return null.
function reversePlaylist(head) {
let prev = null;
let current = head;
while (current !== null) {
const next = current.next; // save the way forward
current.next = prev; // flip the arrow
prev = current; // step forward
current = next;
}
return prev;
}def reverse_playlist(head):
prev = None
current = head
while current is not None:
nxt = current.next # save the way forward
current.next = prev # flip the arrow
prev = current # step forward
current = nxt
return prevRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.