FastPrepMerge Sort a Linked List
Problems/Merge Sort a Linked List

Merge Sort a Linked List

Zillow logoZillow● MediumINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported at Zillow in a February 2013 review by a Software Development Engineer Intern candidate in Seattle who got an offer. The candidate's process began on campus with an email to Zillow afterwards; next came a pair of coding problems sent to them, then a phone interview, then an invitation to an onsite interview expected to take two to four hours. Each of the three onsite interviewers went over the candidate's resume before working through code with them; one of those coding tasks was to build merge sort with a pair of linked lists.

That wording fits two tasks: merging a pair of linked lists that are already sorted, or sorting one linked list by merge sort, which splits it into two lists and merges them back. We read it as the second, and that reading is ours; merging two sorted lists is the helper step the editorial explains. The report does not give a function shape, an input format or limits. Our choices: the function sortList gets the first node of one singly linked list of integers and returns the first node of the same nodes in ascending order; the list may be empty; the limits (0 to 50,000 nodes, values from -100,000 to 100,000) are ours too. A judge sees the returned list and nothing else, so the page asks for merge sort but cannot check which method is used.

Our version is about 80% the same as the reported question.

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) → ListNode

Examples

Example 1

head = [6,-1,4,2]return = [-1,2,4,6]
head
6-142
→
return
-1246
Moved or new

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]
head
31301
→
return
01133
Moved or new

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 = []
head
empty
return
empty

The list has no nodes, so head is null and the function returns null, which the examples write as [].

Constraints

  • The list has between 0 and 5 * 104 nodes.
  • -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
All Zillow interview questionsSee Zillow hiring insights
Saved
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode sortList(ListNode head) {
        // Write your code here.
    }
}
Checking account…