Reverse a YouTube Music Queue Stored as a Singly Linked List
Last updated: November 29, 2026
Quick Overview
A play queue is stored as a singly linked list of track IDs. Reverse the queue in place and return the new head, first iteratively and then recursively.
0
0
0 solved
A play queue is stored as a singly linked list of track IDs. Reverse the queue in place and return the new head, first iteratively and then recursively.
A classic Google phone screen warm-up that the interviewer uses to check pointer fluency before moving to a harder follow-up. They watch whether you can rewire next pointers without losing the rest of the list and whether you can explain the recursive version's stack cost.
What the Interviewer Expects
- Use three pointers (prev, cur, nxt) and save next before rewiring
- Return the old tail as the new head
- Handle empty and single-node queues without special-case crashes
- Explain why recursion uses O(n) stack and can overflow on long queues
- Trace a three-node example out loud
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Reverse only the first k tracks and leave the rest in order.
- How would you reverse a doubly linked queue, and what changes?
- A queue has 10 million tracks. Which version do you ship and why?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input: head of a singly linked list (possibly None).
- Output: head of the reversed list, reusing the same nodes.
- O(1) extra space for the iterative version; no copying values into an array....
High-Level Design
Walk the list once. At each node, remember its successor, point it backward at the node behind it, then step forward. When the walk ends, the last node visited is the new head. The recursive version r...