As reported: Amazon SDE (2025 graduate) round 2 (July 2025): Sort List, followed by three or four follow-ups, each needing code, a dry run and the complexity. The last 10 minutes were Leadership Principles.
Given the head of a linked list, return the list sorted in ascending order. Aim for O(n log n) time.
This is LeetCode 148 — Sort List.
Example 1
head = [4,2,1,3]
Output: [1,2,3,4]
Example 2
head = [-1,5,3,4,0]
Output: [-1,0,3,4,5]
Constraints: 0 ≤ number of nodes ≤ 5·10⁴; −10⁵ ≤ Node.val ≤ 10⁵.
Code runner format: one line: the list's values as a JSON array. Implement sortList(head); the harness prints the sorted list as a JSON array.
Follow-ups
- Sort ascending or descending depending on an option.
- Sort by the number of set bits of each value.
- Additionally, sort by the number of zeros.