Merge Sort a Linked List
Updated
Problem statement
You get head, the first node in a singly linked list of integers; head is null when the list has no nodes. Reorder the nodes so the values run from smallest to largest, then hand back the first node of the reordered list.
Practice merge sort here. Cut the list into two shorter lists of about equal length (with an odd count, the first part holds the extra node), sort each part the same way, then merge the two sorted parts: walk both at once and keep linking whichever front node holds the smaller value. A list with zero nodes or one node needs no work.
Duplicate values can appear. Each input node must appear exactly once in what you return. Grading compares the values of your returned list with the expected values and nothing else, so it cannot tell which sorting method you wrote.
The examples write each list as its node values from the first node to the last, and [] stands for a list with no nodes.
Function
sortList(head: ListNode) → ListNodeExamples
Example 1
head = [6,-1,4,2]return = [-1,2,4,6]Cutting this list of length 4 in the middle gives [6,-1] and [4,2]. Sorted, those parts are [-1,6] and [2,4]. The merge compares the front values -1 and 2 and links -1, then 6 against 2 links 2, then 6 against 4 links 4; the second part is used up, so 6 is linked last.
Example 2
head = [3,1,3,0,1]return = [0,1,1,3,3]With five nodes the first part holds three: [3,1,3] and [0,1]. Sorted, they are [1,3,3] and [0,1]. Merging them keeps both 1 values and both 3 values, so all five nodes come back as [0,1,1,3,3].
Example 3
head = []return = []The list has no nodes, so head is null and the function returns null, which the examples write as [].
Constraints
- The list has between
0and5 * 104nodes. -105 <= Node.val <= 105
More Zillow Onsite Interview questions
- Valid ParenthesesPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2025
- Build a Hash TablePHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2023
- Spiral MatrixPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2022
- First Subarray with the Largest SumOA · ONSITE INTERVIEW · Seen Aug 2021
- Count Duplicate CharactersONSITE INTERVIEW · Seen Oct 2020
- Decision Tree Classifier from ScratchONSITE INTERVIEW · Seen Nov 2019
- Find Median from Data StreamPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2018
- LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2016
- Tic-Tac-Toe Winner on a Huge BoardPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2016
- Most Frequent Three-Page SequencesPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2016
Similar questions at other companies
- Sort a Linked List in Descending OrderMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Sort a Linked ListAdobe · PHONE SCREENAdobe interview questions
- Merge K Sorted Linked ListsBharatPe · PHONE SCREEN
- Merge Sort Report with a Noisy ComparatorMercor · ONSITE INTERVIEWMercor interview questions
- K Largest Elements with QuickselectAMD · ONSITE INTERVIEWAMD interview questions
- Implement Merge SortSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Nested Set Structural EquivalencePinterest · PHONE SCREEN · ONSITE INTERVIEWPinterest interview questions
- Flatten a Multilevel Doubly Linked ListBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Sort an ArrayBank of America · ONSITE INTERVIEWBank of America interview questions
- Reverse Linked ListAutodesk · ONSITE INTERVIEWAutodesk interview questions