Reverse Linked List and Nth Highest Salary
Filed under Accolite
Free read 1 of 3Track `prev` (initially `None`), `curr` (initially `head`), and temporarily store `nxt = curr.next`.
The question
Given the head of a singly linked list, reverse the list in-place and return the reversed list's head.
Constraints
- The number of nodes in the list is in the range [0, 5000]
- -5000 <= Node.val <= 5000
Examples
- Input
- head = [1, 2, 3, 4, 5]
- Output
- [5, 4, 3, 2, 1]
- Why
- The links are reversed so 5 points to 4, down to 1 pointing to None.
- Input
- head = []
- Output
- []
- Why
- An empty list reverses to an empty list.
Approach
Track `prev` (initially `None`), `curr` (initially `head`), and temporarily store `nxt = curr.next`.
Set `curr.next = prev`, shift `prev = curr`, and advance `curr = nxt` until `curr` becomes `None`.
For SQL Nth salary: `SELECT DISTINCT salary FROM Employee ORDER BY salary DESC LIMIT 1 OFFSET N-1;`.
Tips
- What they are testing: Tests pointer manipulation in linked lists and boundary handling for empty or single-node lists.
- Common mistake: Losing the reference to `curr.next` before redirecting the pointer, causing broken list traversals or infinite loops.
Related Accolite questions
Collected from public candidate posts and coding forums. Updated Nov 2026.